Close application and launch home screen on Androi

2018-12-31 08:40发布

I have two different activities. The first launches the second one. In the second activity, I call System.exit(0) in order to force the application to close, but the first activity is automatically displayed instead of the application returning to the home screen. How can I avoid this, and get the application to return to the home screen?

21条回答
泛滥B
2楼-- · 2018-12-31 08:55

I solved a similar problem: MainActivity starts BrowserActivity, and I need to close the app, when user press Back in BrowserActivity - not to return in MainActivity. So, in MainActivity:

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "sm500_Rmt.MainActivity";
    private boolean m_IsBrowserStarted = false;

and then, in OnResume:

    @Override
protected void onResume() {
    super.onResume();
    if(m_IsBrowserStarted) {
        Log.w(TAG, "onResume, but it's return from browser, just exit!");
        finish();
        return;
    }
    Log.w(TAG, "onResume");

... then continue OnResume. And, when start BrowserActivity:

    Intent intent = new Intent(this, BrowserActivity.class);
    intent.putExtra(getString(R.string.IPAddr), ip);
    startActivity(intent);
    m_IsBrowserStarted = true;

And it looks like it works good! :-)

查看更多
孤独寂梦人
3楼-- · 2018-12-31 08:57

The easiest way for achieving this is given below (without affecting Android's native memory management. There is no process killing involved).

  1. Launch an activity using this Intent:

    Intent intent = new Intent(this, FinActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(intent);
    finish();
    
  2. In the target activity FinActivity.class, call finish() in onCreate.

Steps Explained:

  1. You create an intent that erases all other activities (FLAG_ACTIVITY_CLEAR_TOP) and delete the current activity.

  2. The activity destroys itself. An alternative is that you can make an splash screen in finActivity. This is optional.

查看更多
与风俱净
4楼-- · 2018-12-31 09:00

It is not recommended, but you can still use this. Better go with this solution in case you need to quit the app.

According to me, the best solution is to finish every activity in your app like below.

Step 1. Maintain a static variable in mainactivity. Say,

public static boolean isQuit = false;

Step 2. On click event of an button, set this variable to true.

mainactivity.isQuit = true;
finish();

Step 3. And in every activity of your application, have the onrestart method as below.

@Override
protected void onRestart() {
    // TODO Auto-generated method stub
    super.onRestart();
    if(mainactivity.isQuit)
        finish();
}
查看更多
泛滥B
5楼-- · 2018-12-31 09:01

Try the following. It works for me.

ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1); 
ComponentName componentInfo = taskInfo.get(0).topActivity;
am.restartPackage(componentInfo.getPackageName());
查看更多
大哥的爱人
6楼-- · 2018-12-31 09:01

Run the second activity using start activity for result:

Intent intent = new Intent(FirstActivity.this, SecondActivity.class);

//This line is important
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);

startActivityForResult(intent, REQUEST_CODE);

Add this function to the first Activity:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(rquestCode == REQUEST_CODE)
        if(resultCode == RESULT_CANCELED)
            finish();
}

And add this to the second Activity:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)  {
    if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {

        Log.i(TAG, "Back key pressed");
        setResult(RESULT_CANCELED);
        finish();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}
查看更多
心情的温度
7楼-- · 2018-12-31 09:02

You can not do System.exit(), it's not safe.

You can do this one: Process.killProcess(Process.myPid());

查看更多
登录 后发表回答