Question

Consider i am using five screen pages for project "A".Each page is having switching between other pages sequentially one by one,my need is to do close all the page when i am clicking the button "exit" from the page five which is the last one.

I have used this below code,but the problem is only the last page is getting close others are not.

find my code below

Button extbtn = (Button)findViewById(R.id.but_Exit);
extbtn.setOnClickListener(new View.OnClickListener() {

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

Thanks for your time!

Was it helpful?

Solution

Make all five activities extend a BaseActivity that registers a BroadcastReceiver at onCreate (and unregisters at onDestroy). When extbtn is clicked, send a broadcast to all those BaseActivities to close themselves

for example, in your BaseActivity add:

public static final String ACTION_KILL_COMMAND = "ACTION_KILL_COMMAND";
public static final String ACTION_KILL_DATATYPE = "content://ACTION_KILL_DATATYPE";

private KillReceiver mKillReceiver;

@Override
protected void onCreate(Bundle savedInstanceState)  {
    ...
    ...
    mKillReceiver = new KillReceiver();
    registerReceiver(mKillReceiver, IntentFilter.create(ACTION_KILL_COMMAND, ACTION_KILL_DATATYPE));        
}

@Override
protected void onDestroy() {
    super.onDestroy();
    unregisterReceiver(mKillReceiver);
}

private final class KillReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        finish();
    }
}

and at extbtn's onClick call:

extbtn.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        // send a broadcast that will finish activities at the bottom of the stack
        Intent killIntent = new Intent(BaseActivity.ACTION_KILL_COMMAND);
        killIntent.setType(BaseActivity.ACTION_KILL_DATATYPE);
        sendBroadcast(killIntent);

        Intent intent = new Intent();
        setResult(RESULT_OK, intent);
        finish();
    }
});    
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top