How to finish current activity in Android

前端 未结 8 1408
长发绾君心
长发绾君心 2020-11-28 03:19

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 finis

相关标签:
8条回答
  • 2020-11-28 03:25

    What I was doing was starting a new activity and then closing the current activity. So, remember this simple rule:

    finish()
    startActivity<...>()
    

    and not

    startActivity<...>()
    finish()
    
    0 讨论(0)
  • 2020-11-28 03:27

    You need to call finish() from the UI thread, not a background thread. The way to do this is to declare a Handler and ask the Handler to run a Runnable on the UI 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();
        }
    }
    
    0 讨论(0)
  • 2020-11-28 03:35

    Just call the finish() method:

    context.finish();
    
    0 讨论(0)
  • 2020-11-28 03:38

    I tried using this example but it failed miserably. Every time I use to invoke finish()/ finishactivity() inside a handler, I end up with this menacing java.lang.IllegalAccess Exception. i'm not sure how did it work for the one who posed the question.

    Instead the solution I found was that create a method in your activity such as

    void kill_activity()
    { 
        finish();
    }
    

    Invoke this method from inside the run method of the handler. This worked like a charm for me. Hope this helps anyone struggling with "how to close an activity from a different thread?".

    0 讨论(0)
  • 2020-11-28 03:39

    You can also use: finishAffinity()

    Finish this activity as well as all activities immediately below it in the current task that have the same affinity.

    0 讨论(0)
  • 2020-11-28 03:41

    When you want start a new activity and finish the current activity you can do this:

    API 11 or greater

    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);
    

    I hope this can help somebody =)

    0 讨论(0)
提交回复
热议问题