Reputation: 1922
I'm having a difficulty clearing my activities on stack from a fragment. This is what I've done so far, I have 3 activities, namely: login, main, and profile. The first activity is the login. If successful, the app will go to main activity. Inside main activity, I have a button which goes to profile activity.
LogoutAlertDialog.class
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
OnClickListener positiveClick = new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getActivity().getBaseContext(),
"Logging out...", Toast.LENGTH_SHORT).show();
Intent i = new Intent(getActivity(), Login.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
getActivity().finish();
}
};
OnClickListener negativeClick = new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
};
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage("Are you sure you want to logout?");
builder.setNegativeButton("No", negativeClick);
builder.setPositiveButton("Yes", positiveClick);
builder.setIcon(R.drawable.logout);
builder.setTitle("Logout");
Dialog dialog = builder.create();
return dialog;
}
Profile.class:
/**
* On selecting action bar icons
* */
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Take appropriate action for each action item click
switch (item.getItemId()) {
case R.id.action_logout:
edit = shared.edit();
edit.putString("Username", "");
edit.putString("isLoggedIn", "no");
edit.commit();
FragmentManager fm = getFragmentManager();
LogoutAlertDialog alert = new LogoutAlertDialog();
alert.show(fm, "");
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_main_actions, menu);
return super.onCreateOptionsMenu(menu);
}
But when I tried this, the app went to my Login.class - Activity but when I clicked the back button, it was going back to the main activity.
Does anybody know what's wrong with my code? Any help is pretty much appreciated. Thanks.
UPDATE:
I have set this:
<activity
android:name="com.example.sample.Profile"
android:label="@string/myprofile"
android:parentActivityName="com.example.sample.MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
in my AndroidManifest because I'm using an ActionBar which lets user go back to its parent activity. Is there an issue with that?
Upvotes: 0
Views: 66
Reputation: 574
Use This
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Upvotes: 2