png
png

Reputation: 4408

Android : Exit application

I went throgh different questions realted to this topic and still have few doubts.

Que One: In my application i have multiple activities A->B->C

A->B2->C2.. A-B3-C3...

is the flow . I have "back" and "home" button for each activity to go to previous activity and A respectively.

Initially i was launching each of these as "single task" and everything appeared to be good.

Now i wanted to handle error condition and thus the exit application came to picture. SO from my reading i understand its not "good" to launch each activity as single task ( i ma not very clear the reason or am i wrong here ) SO i changed all activities launch mode as standard So one thing i noticed is switching between actvities throu "back" or "home" is slow than earlier. SO iam i doing the right thing here

Que two:now i have my code

    if (some error)
    {
        this.finish()       
    }

    code line;

this is in my root activity A and will be hit before launching any other activity But what i see is finish is executed but before application really exit , code line is executed leading to some exception. My expected behaviour is once i call finish , it is like a return from this activity and no more code executed here

Que 3: Now when we call a finish on the root activity, who will actually handle it.

Upvotes: 0

Views: 4643

Answers (2)

Lazy Ninja
Lazy Ninja

Reputation: 22537

Close all the previous activities as follows:

    Intent intent = new Intent(this, MainActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.putExtra("Exit me", true);
    startActivity(intent);
    finish();

Then in MainActivity onCreate() method add this to finish the MainActivity

    setContentView(R.layout.main_layout);

    if( getIntent().getBooleanExtra("Exit me", false)){
        finish();
        return; // add this to prevent from doing unnecessary stuffs
    }

Upvotes: 0

Android
Android

Reputation: 2393

Intent i = new Intent();
i.setAction(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_HOME);
ListActivity.this.startActivity(i); 
finish();   

Upvotes: 7

Related Questions