Close application and launch home screen on Android

后端 未结 21 2033
我在风中等你
我在风中等你 2020-11-22 07:33

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, bu

21条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-22 08:25

    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! :-)

提交回复
热议问题