1

Assume i have four classes.Each class is having a button to do screen switching from one page to another page,well its working fine.But now im trying to close all the activities by clicking the button from the last page.I have tried that too, but its just closing only the last page.How to achieve this concept?

Please find my code for reference

Xit_demoActivity.java

public class Xit_demoActivity extends Activity {
/** Called when the activity is first created. */

Button btn1; 

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

    btn1 = (Button)findViewById(R.id.button_1);
    btn1.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {
    Intent myIntent = new Intent(v.getContext(), Class1.class);
    startActivity(myIntent);
        }
    });

Class1.java

public class Class1 extends Activity 
{

Button btn1;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main1);

    btn1 = (Button)findViewById(R.id.button_2);
    btn1.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {
    Intent myIntent = new Intent(v.getContext(), Class2.class);
    startActivity(myIntent);
        }
    });

Class2.java

public class Class2 extends Activity 
{

Button btn1;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main2);


    btn1 = (Button)findViewById(R.id.button_3);
    btn1.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {
    Intent myIntent = new Intent(v.getContext(), Class3.class);
    startActivity(myIntent);
        }
    });

Class3.java

public class Class3 extends Activity 
{
@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main3);

    Button next = (Button) findViewById(R.id.button_4);
    next.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) 
    {
        Intent intent = new Intent();
        setResult(RESULT_OK, intent);
        finish();
        }
    });

Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.and.roidu.xit"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="8" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:name=".Xit_demoActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity><activity android:name=".Class1"></activity>

    <activity
        android:name=".Class1"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN1" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity><activity android:name=".Class2"></activity>



    <activity
        android:name=".Class2"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN2" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity><activity android:name=".Class3"></activity>

    <activity
        android:name=".Class3"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN3" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>


 </application>

</manifest>

Thanks for your time!..

Android app_devlpr
  • 63
  • 1
  • 2
  • 12
  • When you call the finish() method in the last onClick method of Class 3 then only that current activity is closed. To close all other activities, the finish() method must be called on those particular activities. – Antrromet Jul 11 '12 at 10:10

4 Answers4

5

Simply try this. Consider your Class3.java is your last Activitiy And, in Button of next use below code -

Intent intent = new Intent(currentActivity.this, XitActivity.class); // instead of XitActivity use your first activity
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("EXIT", true);
startActivity(intent);

And, in your Xit_demoActivity's onCreate() use below code -

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

Hope this will helps you to exit you from all activities.

Praveenkumar
  • 24,084
  • 23
  • 95
  • 173
  • 1
    Good answer - beat me to it by seconds! One note it is almost always wrong to use `getApplicationContext()` - there is a SO question here that discusses this http://stackoverflow.com/questions/7298731/when-to-call-activity-context-or-application-context – Graham Smith Jul 11 '12 at 10:09
  • @GrahamSmith Check my updated answer. Is that okay now? Using `currentActivity.this` instead of `getApplicationContext()` – Praveenkumar Jul 11 '12 at 10:11
  • @Spk if follow your answer it just calling the first class.java,but i need to close the whole application when im cliking that button,how to do this? – Android app_devlpr Jul 11 '12 at 10:15
  • @Androidapp_devlpr Check my updated answer and paste this code `if (getIntent().getBooleanExtra("EXIT", false)) { finish(); }` in your `Xit_demoActivity` java's `onCreate()` method. – Praveenkumar Jul 11 '12 at 10:20
1

I would insist to create an BaseActivity with a BroadCastReceiver that will notify when you want to finish all the Activities and extend all the Activity classes with this BaseActivity,

public static final String ACTION_FINISH = "ACTION_FINISH";
public class BaseActivity extends Activity {

public void onCreate(Bundle bundle, int resourceId) {
    super.onCreate(bundle);
    registerReceiver(finishActivitiesReceiver, 
                                          new IntentFilter(ACTION_FINISH));
}

protected void onDestroy() {
    super.onDestroy();
    unregisterReceiver(finishActivitiesReceiver);
}

BroadcastReceiver finishActivitiesReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            finish();
        }
    };
}

Then you can just send BroadCast when you want to close all Activities using

sendBroadcast(new Intent(BaseActivity.ACTION_FINISH));
Lalit Poptani
  • 67,150
  • 23
  • 161
  • 242
0

You should not do this. If you want to open home screen or previous app screen. You can simply call

moveTaskToBack(boolean bool);

it will send your app to background. or if you want to not have class1 2 and 3 in stack you can use excludeFromRecent flag in manifest decl's of activities.

This thread got thousands of views, for the same issue. have a look on it and make a understanding.

Is quitting an application frowned upon?

Community
  • 1
  • 1
AAnkit
  • 27,299
  • 12
  • 60
  • 71
0

On the click of button in Class 3 write this:

Intent intent=new Intent(Class3.this,Class1.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
Praveenkumar
  • 24,084
  • 23
  • 95
  • 173
AkashG
  • 7,868
  • 3
  • 28
  • 43