Close all running actions in the Android application.

I create one application and never use the finish () for each action. If my user clicks the exit button, it goes to the previous page.

How to close the previous activity and close the application?

+5
source share
11 answers

This is one workaround I tried and it worked fine for me.

SOLUTION-1

this.finish();//try activityname.finish instead of this
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

use this in the action in which you want to exit the application.

==================================================== ================================= The above code helps to resume your application where you last stayed.

SOLUTION-2

, , .

onActivityResult()

, , 3 A, B, C, A- > B- > C, C , .

A

public class A extends Activity {
int Finish = 100;
Button launch_second;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    launch_second = (Button) findViewById(R.id.start_second_act);
    launch_second.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent second = new Intent(A.this,
                    B.class);
            startActivityForResult(second, Finish);
        }
    });
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
    case 100:
        this.finish();
        break;
    default:
        break;
    }
    super.onActivityResult(requestCode, resultCode, data);

}

B

public class B extends Activity {
private Button launch_next;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.second_main);
    launch_next = (Button) findViewById(R.id.start_third_activity);
    launch_next.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent third = new Intent(B.this,C.class);
            startActivityForResult(third, 100);
        }
    });
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
    case 100:
        setResult(requestCode);
        this.finish();
        break;

    default:
        break;
    }
    super.onActivityResult(requestCode, resultCode, data);
}

}

C

public class C extends Activity {
Button kill_app;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.third_main);
    kill_app = (Button)findViewById(R.id.kill_app_btn);
    kill_app.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            C.this.finish();
            setResult(100);

        }
    });

}

}

-3

finishAffinity(), , .

+6

, .

:

Intent intent = new Intent(this,MyHomeActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putBooleanExtra("finishApplication", true);
startActivity(intent);

MyHomeActivity ( ) onResume:

if(getIntent().getBooleanExtra("finishApplication", false){
   finish();
}

, Home.


Dirtier:

- (, , );

public static boolean loggingOut = false;

, true .

YourApplication.loggingOut = true;
finish();

onResume()

if(loggingOut){
   finish();
}

, boolean false / :

if(loggingOut){
   finish();
   YourApplication.loggingOut = false;
}

, "" , onBackPressed(),

@Override
public void onBackPressed(){
     YourApplication.loggingOut = true;
     super.onBackPressed();
}
+3

finish()

1)

2)

3) (2 - (+1) Intent.FLAG_ACTIVITY_CLEAR_TOP)

+1

. .

: , , finish() , onResume(), , null finish(), , . .

+1
Intent intent = new Intent(this, LoginActivity.class);
ComponentName cn = intent.getComponent();
Intent mainIntent = IntentCompat.makeRestartActivityTask(cn);
startActivity(mainIntent);

:

public static Intent makeRestartActivityTask (ComponentName mainActivity)

, . makeMainActivity (ComponentName), FLAG_ACTIVITY_NEW_TASK FLAG_ACTIVITY_CLEAR_TASK.

+1
@Override
    public void onClick(View v) {

        if (v == btnAtGlanceBack) {
            Intent backIntent = new Intent(this, Dashboard.class);
            startActivity(backIntent);

        } else if (v == btnAtGlanceLogOut) {
            Intent logoutIntent = new Intent(this, GlobalScholarLogin.class);
            logoutIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(logoutIntent);

        }
    }
0

Android , . , Intent.FLAG_ACTIVITY_CLEAR_TASK , .

0

, Android,

Intent intent = new Intent(this, MyHomeActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        finish();
        startActivity(intent);
0

System.exit(2);

0

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

?

0

You can add a list in the application, which saves all the actions you created. and when you go out, you just need to complete all the activities on the list.

0
source

All Articles