Restarting Android app programmatically

前端 未结 9 1991
独厮守ぢ
独厮守ぢ 2021-02-03 12:14

This is a follow up question to this question:

Force application to restart on first activity

I am trying to restart my application from a fragment like that:

相关标签:
9条回答
  • 2021-02-03 12:49

    As the accepted answer doesn't seem to work on Android Q anymore due to the new background launch limitations, let me add my own solution:

    startActivity(
        Intent(applicationContext, MainActivity::class.java)
    )
    System.exit(0)
    

    Although it looks strange, it works very reliably for me, as the intent takes longer to fire than the system exit does. Plus, it is fired from the main thread, so Android Q won't complain.

    0 讨论(0)
  • 2021-02-03 12:51

    Please refer below code

        Intent intent = new Intent(this, YourHomeActivity.class);
        this.startActivity(intent);
        this.finishAffinity();
    

    It is starting your home activity and dismiss all other activities. It looks like a restart to users, but the process is the same.

    0 讨论(0)
  • 2021-02-03 12:58
    Intent i = getBaseContext().getPackageManager()
                 .getLaunchIntentForPackage( getBaseContext().getPackageName() );
    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    finish();
    startActivity(i);
    

    You can refer to previous discussion (click here)

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