Question

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

Was it helpful?

Solution 2

  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.

OTHER TIPS

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();
}

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

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.

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;
    }

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top