Question

I am navigating the Activity 2, and by the time I press the back button, it should exit or finish the application without displaying the main activity. I am using ViewPager(Activity2), the code is working in backpress if I only navigate in the first page of the ViewPager, the moment I navigate the other pages, the code for exiting or finishing the application doesn't work and displays the mainActivity.

MainActivity

Intent myIntent = new Intent(MainActivity.this, Activity2.class);

Activity2

@Override
public void onBackPressed() {
      super.onBackPressed();
    this.finish();
    android.os.Process.killProcess(android.os.Process.myPid());
    System.exit(0);
    getParent().finish();
    Activity a1 = (Activity)this.getBaseContext();

    if(this.getParent()!=null){
        Activity a = (Activity)this.getParent().getApplicationContext();
        a.finish();
    }

    Log.i("Backpressed", "pressed");

}
Was it helpful?

Solution

This would work,

    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_HOME);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);

OTHER TIPS

are you sure that after "System.exit(0);" is called, the rest of the code will get called? as i remember, it won't. it should work as you wanted though.

anyway, please don't use any of those methods. the System.exit might be enough for you on some cases, but it in fact kills all of the threads of the app and considered a dirty way to do it.

instead, call startActivityForResult from Activity 1 to Activity 2 . then, in activity 2 call finish and set the result to something, and on activity 1 check if the result is to exit the app, and if so, call finish() there too.

Try to add this android:noHistory="true" into your manifest like this

<activity
            android:noHistory="true"
            android:name="com.example.activities.MainPageActivity"
            android:configChanges="orientation|keyboardHidden|screenSize|screenLayout"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.NoTitleBar" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

add finish(); in your MainActivity.
You can also Add the below lines in you Manifest under MainActivity

        android:excludeFromRecents="true"
        android:finishOnTaskLaunch="true"
        android:noHistory="true"

if you are using kill process..then on backpresses kill all the process..use a for loop to get pid for all the processes..and kill them..like this

ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningAppProcessInfo> list = am
            .getRunningAppProcesses();


    if (list != null) {
        for (int i = 0; i < list.size(); ++i) {
            String processid = list.get(i).processName;


            String packageName = mContext.getPackageName();
            if (processid.contains(packageName + ":homescreen")) {
                //here homescreen is the name you given in manifest for android:process

                Process.killProcess(list.get(i).pid);

            } 

and in manifest for each activity add android:process=":anyname" like this

android:process=":homescreen"

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