0

I have used alarm manager to launch stage2 at specific time and whenever app will receive broadcast from alarm manager it will launch appropriate activity. When app is running in stage1 and broadcast received I have used following coed to launch appropriate activity

Intent i = new Intent(context, StageTwo.class);
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(i);

this code launches activity fine but when back key is pressed previous stage1 activity resumes I want to exit from app on pressing back button as user is not allowed to go back when activity is launched

pleas help as I am stuck at this point and can't find solution

HemangNirmal
  • 621
  • 1
  • 8
  • 24

5 Answers5

0

Just simply you can finish your current activity when you start second activity:

look at this code

    Intent i = new Intent(context, StageTwo.class);
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    finish();
    context.startActivity(i);

Good Luck..

WonderSoftwares
  • 2,800
  • 1
  • 15
  • 21
0

As given here

Android's design does not favor exiting an application by choice, but rather manages it by the OS. You can bring up the Home application by its corresponding Intent:

try over ridding the onBack pressed even like this and launch the home screen

@Override
    public void onBackPressed() {
        // TODO Auto-generated method stub
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

    }

And like the title suggests if you want to exit all the activities try this method

1.Create a class called ActivityListHolder

2.create an array list of activities like

List<Activity> activities=new ArrayList<Activity>(); (make it static)

3.on Every activities on create add the activity to the list by ActivityListHolder.activities.Add(this)

4.And whenever you want to finish the activities you can loop through the list and check it is not the current activity and try to call finish() on that object.

Community
  • 1
  • 1
insomniac
  • 11,146
  • 6
  • 44
  • 55
0
public abstract class AppBaseActivity extends Activity {

public static final String FINISH_ALL_ACTIVITIES_ACTIVITY_ACTION = "com.myActivity.FINISH_ALL_ACTIVITIES";

private BaseActivityReceiver baseActivityReceiver = new BaseActivityReceiver();

public static final IntentFilter INTENT_FILTER = createIntentFilter();

private static IntentFilter createIntentFilter() {
IntentFilter filter = new IntentFilter();
filter.addAction(FINISH_ALL_ACTIVITIES);
return filter;
}

protected void registerBaseActivityReceiver() {
registerReceiver(baseActivityReceiver, INTENT_FILTER);
}

protected void unRegisterBaseActivityReceiver() {
unregisterReceiver(baseActivityReceiver);
}

public class BaseActivityReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(FINISH_ALL_ACTIVITIES_ACTIVITY_ACTION)) {
finish();
}
}
}

protected void closeAllActivities() {
sendBroadcast(new Intent(FINISH_ALL_ACTIVITIES_ACTIVITY_ACTION));
}
}
CodeWarrior
  • 5,026
  • 6
  • 30
  • 46
0

In Api level 16 and above you can use

finishAffinity();

method and below it you have to save all created activity list manually and finish all one by one. no second option below API 16 :(. Then Start Your launching Activity. Thats it....

Jayesh Khasatiya
  • 2,140
  • 1
  • 14
  • 15
0

I solve this problem, I think it was my silly mistake as I forgot activity life cycle

I just call finish() method in onPause() method of stage1 activity when stage2 activity is launched by broadcast receiver and problem is solved now.

HemangNirmal
  • 621
  • 1
  • 8
  • 24