How to complete the current work in Android

I have an android app. I am making a loading screen with a progress bar.

I entered a delay in the onCreate method. When the timer ends, I want to end 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 complete?

+58
android activity-finish
Feb 15 '11 at 7:11
source share
5 answers

If you are making a boot screen, simply set the parameter so that it does not save it in the action stack. In your manifest.xml, where you define your activity, follow these steps:

 <activity android:name=".LoadingScreen" android:noHistory="true" ... /> 

And in your code there is no need to call .finish (). Just do startActivity (i);

There is also no need to store an instance of your current activity in a separate field. You can always access it like LoadingScreen.this.doSomething() instead of private LoadingScreen loadingScreen;

+105
Feb 15 '11 at 7:19
source share

I tried to use this example, but it failed. Every time I use the finish () / finishactivity () function inside the handler to call, I get this threatening java.lang.IllegalAccess Exception . I'm not sure how this works for the one who asked the question.

Instead of the solution I found was to create a method in your activity, for example

 void kill_activity() { finish(); } 

Call this method from the run method of the handler. It worked for me like a charm. Hope this helps anyone struggling with “how to close activity from another thread?”.

+41
Oct 05 '11 at 19:23
source share

You need to call finish() from the user interface thread, not the background thread. The way to do this is to declare a handler and ask the handler to run Runnable in the user interface thread. For example:

 public class LoadingScreen extends Activity{ private LoadingScreen loadingScreen; Intent i = new Intent(this, HomeScreen.class); Handler handler; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); handler = new Handler(); setContentView(R.layout.loading); CountDownTimer timer = new CountDownTimer(10000, 1000) //10seceonds Timer { @Override public void onTick(long l) { } @Override public void onFinish() { handler.post(new Runnable() { public void run() { loadingScreen.finishActivity(0); startActivity(i); } }); }; }.start(); } } 
+10
Feb 15 '11 at 7:20
source share

Just call finish() method:

 context.finish(); 
+8
Mar 04 '15 at 3:27
source share

If you want to start a new activity and complete an ongoing activity, you can do this:

API 11 or higher

 Intent intent = new Intent(OldActivity.this, NewActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); startActivity(intent); 

API 10 or lower

 Intent intent = new Intent(OldActivity.this, NewActivity.class); intent.setFlags(IntentCompat.FLAG_ACTIVITY_NEW_TASK | IntentCompat.FLAG_ACTIVITY_CLEAR_TASK); startActivity(intent); 

Hope this can help someone =)

+5
May 16 '16 at 7:00 a.m.
source share



All Articles