Question

I have two activities.

  1. MainActivity
  2. ListActivity

After some job in MainActivity, ListAllActivity starts. I want to exit the application when back button pressed from ListAllActivity. And my code for this is,

@Override
public void onBackPressed() {
    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_HOME);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(intent);
    super.onBackPressed();
}

But, when I'm pressing the back Button from ListAllActivity , it simply brings me back to MainActivity.

The AndroidManifest entry for these Activities are :

<activity
        android:name="com.example.myapps.MainActivity"
        android:label="@string/loading" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.example.myapps.ListAllActivity"
        android:label="@string/title_activity_list_all" >

    </activity>

Please help.

Was it helpful?

Solution

Follow the below steps

  • From the MainActivity you need to call the finish() method after you pass intent to ListAllActivity
  • In the ListAllActivity call the finish() method on your button click or just press the back button as back button by default call finish() method.
  • In this way you will exit from your app.

On the next button click of MainActivity write below code

Intent intent=new Intent(MainActivity.this,ListAllActivity.class);
startActivity(intent);
finish();

and on the ListAllActivity back button write below code

finish();

OTHER TIPS

Try to finish your MainActivity before your start your ListAllActivity and finish your ListAllActivity on onBackpressed as below:

Supposing you are starting your ListAllActivity from mainactivity.

public class MainActivity extends Activity
{
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

     ....................
      //Supposing you are starting activity on button click write as below in your onclick event. 
     Intent intent = new Intent(MainActivity.this, ListActivity.class);
      startActivity(intent);
    finish();

  }
}

In your ListAllActivity

@Override
public void onBackPressed() {
    super.onBackPressed();
    finish();
}

When you fire intent from MainActivity to ListActivity then call finish(); like

Intent intent = new Intent(MainActivity.this, ListActivity.class);
startActivity(intent);
finish();
Intent intent = new Intent(getApplicationContext(), ListActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

Pass intent like this to start the list activity from first activity and while u press back in the list activity app will exit.

This is all you need here

@Override
public void onBackPressed ()
{       
    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_HOME);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
}

Just use my snippet in onBackPressed() to kill the process using process id. Its simple and much more efficient.

@Override
    public void onBackPressed() {
        // TODO Auto-generated method stub
        android.os.Process.killProcess(android.os.Process.myPid());
    }

You just need to call finish() method:

Intent intent=new Intent(MainActivity.this,ListAllActivity.class);
startActivity(intent);
finish();

Also on the ListAllActivity back button write below code:

finish();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top