Restart Android activity on relaunch of application

十年热恋 提交于 2019-12-13 04:58:55

问题


I had 3 activities in an android application. The application will exit when I press back button in each activity. Using the following code.

When I press back from the third activity, the application exits fine but when I relaunch the application by clicking the app icon, then the third activity will launch again. But I need to launch my main activity at the time of such "relaunch".

I tried write the code on "onResume" but not working.

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) 
{
    if (keyCode == KeyEvent.KEYCODE_BACK) 
    {
        moveTaskToBack(true);
        return true;
    }
        return super.onKeyDown(keyCode, event);
}

Please help me. Thanks in advance


回答1:


  1. Create three activities - A, B and C
  2. In Activity A - when calling startActivity(B), call finish() also. Example -

     public void onButtonClick() // Some method 
     {
        startActivity(intentForB);
        finish();
    }
    
  3. Similarly when going to C from B -

     public void onButtonClick()
     {
        startActivity(intentForC);
        finish();
     }
    
  4. When the user is on Activity C and when he presses the back button , the application will get closed.(No need to write back button handling explicitly).

Hope this helps.




回答2:


You can solve this problem by using following Way In your third Activity class put following Code,

    @Override
public void onBackPressed() {
    // TODO Auto-generated method stub
    super.onBackPressed();
    startActivity(new Intent(getBaseContext(), YourSecondActivity.class));
    finish();
}

Same way You can put in second Activity Class

    @Override
public void onBackPressed() {
    // TODO Auto-generated method stub
    super.onBackPressed();
    startActivity(new Intent(getBaseContext(), YourFirstActivity.class));
    finish();
}

Finally You can put this in your main class

    @Override
public void onBackPressed() {
    // TODO Auto-generated method stub
    super.onBackPressed();
    finish();
}



回答3:


If you want to exit in all 3 Activities, then you must close the current Activity using finish() while starting a new Activity.




回答4:


moveTaskToBack() Move the task containing this activity to the back of the activity stack.

Do it in OnBackpressed() and use finish() in all your activities. (Because you said you want to finish all the activities when you have pressed back button)

public void OnBackpressed()
{
 finish();
}

and Simply, in your code do like this:

In activity 1

Intent intent=new Intent(activity1.this,activity2.class);
startActivity(intent);
finish();

In activity 2

Intent intent=new Intent(activity2.this,activity3.class);
startActivity(intent);
finish();

If you used this in your first two activities, Then in third activity no need to handle OnBackpressed(). Because OnBackpressed() is Called when the activity has detected the user's press of the back key. The default implementation simply finishes the current activity, but you can override this to do whatever you want.




回答5:


Use this code in your AndroidManifest.xml and android:clearTaskOnLaunch="true" in first launch activity.

 <activity
        android:name="com.example.package.SplashActivity"
        android:label="@string/app_name"
        android:clearTaskOnLaunch="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

If you want to use Key Event then use this code in your activity :

@Override
    public boolean dispatchKeyEvent(KeyEvent event)
    {
        boolean result = false;
        switch(event.getKeyCode())
        {
            case KeyEvent.KEYCODE_BACK:
                finish(); // or moveTaskToBack(true);
                result = true;
                break;
             default:
                result= super.dispatchKeyEvent(event);
                break;
         }
        return result;
    }



回答6:


Use this code in your AndroidManifest.xml and android:launchMode="singleTop" in first launch activity.



来源:https://stackoverflow.com/questions/16475684/restart-android-activity-on-relaunch-of-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!