I'm making a SplashActivity
and I encountered a problem when I want to pass the SplashActivity
context into a static function that run inside a CountDownTimer
enclosed scope using this
keyword and getBaseContext()
method.
TL;DR
I am trying to figure out how to access SplashActivity
context inside an enclosed scope, that is CountDownTimer
.
What I have tried:
SplashActivity temp = this
I have made a SplashActivity temp = this;
declaration on onCreate
method of the Activity
, but decided this will not be effective for all Activity
that I have made, since I must declare Activity
for each Activity
class that I have made.
Context temp = this.getBaseContext();
Basically same as above, but more flexible for all the Activity
but in my opinion is still not effective for the program.
ClassName.staticFunction(super.getBaseContext());
This will not work because CountDownTimer
doesn't extends
from SplashActivity
, although this kind of solution is the one I was looking for.
Codes:
SplashActivity.java
public class SplashActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
//...
new CountDownTimer(3000,100) {
//...
public void onFinish() {
MainActivity.StartActivity( SplashActivity.getBaseContext() );
//SplashActivity. will throw an error, must replace
finish();
}
}.start();
}
}
MainActivity.java
public class MainActivity extends Activity {
...
public static void StartActivity(Context mContext) {
Intent act = new Intent(mContext, MainActivity.class);
mContext.startActivity(act);
}
}
What I expect:
Run MainActivity.StartActivity(something.getBaseContext)
in SplashActivity
CountDownTimer
without creating any additional variable (because memory optimization), creates a MainActivity
view.
For now, I am using this solution:
final Context self = this;
Which is inserted in:
public class SplashActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
final Context self = this; // HERE
//...
new CountDownTimer(3000,100) {
public void onFinish() {
MainActivity.StartActivity( self ); //HERE
...