Java 进入主屏幕后如何重新启动应用程序?

Java 进入主屏幕后如何重新启动应用程序?,java,android,xml,Java,Android,Xml,我使用onBackPressed()将此代码用于销毁活动 我成功地出现在主屏幕上,但当我再次打开应用程序时,应用程序将以恢复状态打开。那么,如何再次重新启动应用程序?请尝试以下操作: Intent intent = new Intent(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_HOME); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); intent.setFlags(Int

我使用onBackPressed()将此代码用于销毁活动

我成功地出现在主屏幕上,但当我再次打开应用程序时,应用程序将以恢复状态打开。那么,如何再次重新启动应用程序?

请尝试以下操作:

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
或者,其他解决方案:

Intent intent = new Intent(MainActivity.this, SplashActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("EXIT", true);
startActivity(intent);
在启动屏幕活动中:

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getIntent().getBooleanExtra("EXIT", false)) {
        finish();
        return;
    }
.....
试试这个:

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
或者,其他解决方案:

Intent intent = new Intent(MainActivity.this, SplashActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("EXIT", true);
startActivity(intent);
在启动屏幕活动中:

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getIntent().getBooleanExtra("EXIT", false)) {
        finish();
        return;
    }
.....

添加finish()或使用intent标志CLEAR_TASK和NEW_TASK您能给我完整的代码Divyesh?odkhe 6 k nai?odkhu j ne yaar tne到jor reputation vadhe 6 tare到mne 1 upvoting kar ne laause此代码
finishafinity();startActivity(新意图(CurrentActivity.this,Home.class));添加finish()或使用intent标志CLEAR_TASK和NEW_TASK您能给我完整的代码Divyesh?odkhe 6 k nai?odkhu j ne yaar tne到jor reputation vadhe 6 tare到mne 1 upvoting kar ne laause此代码
finishafinity();startActivity(新意图(CurrentActivity.this,Home.class));