I have an Android application. I am making a loading screen with a progress bar.
I entered a delay in the onCreate method. When the timer finishes, I want to finish the current activity and start a new one.
It just gives me an exception when it calls the finish()
method.
public class LoadingScreen extends Activity{ private LoadingScreen loadingScreen; Intent i = new Intent(this, HomeScreen.class); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.loading); CountDownTimer timer = new CountDownTimer(10000, 1000) //10 second Timer { public void onTick(long l) { } @Override public void onFinish() { loadingScreen.finishActivity(0); startActivity(i); }; }.start(); } }
How can I change the code so that it ends when the progress bar is done?
Use finish like this: Intent i = new Intent(Main_Menu. this, NextActivity. class); finish(); //Kill the activity from which you will go to next activity startActivity(i);
On Clicking the back button from the New Activity, the finish() method is called and the activity destroys and returns to the home screen.
To Close the Application, you can also take "System. exit(0)" 0 is standard or use any exit code.
If you are doing a loading screen, just set the parameter to not keep it in activity stack. In your manifest.xml, where you define your activity do:
<activity android:name=".LoadingScreen" android:noHistory="true" ... />
And in your code there is no need to call .finish() anymore. Just do startActivity(i);
There is also no need to keep a instance of your current activity in a separate field. You can always access it like LoadingScreen.this.doSomething()
instead of private LoadingScreen loadingScreen;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With