Reputation: 1823
I am facing issue with showing AlertDialog when I switch from one activity to another through navigation(Back button).
scenario is like this: 1. My application is running on and I am on second Page of application. 2. I press back button but at same time for second page one scenario is execute & AlertDialog is open on screen. 3. I already press back button I redirect to main screen & AlertDialog dismiss with out any user input.
Already used this ,but it should not work for me.
1. Intent.FLAG_ACTIVITY_NEW_TASK
2. onRetainNonconfigurationInstance() & getLastNonConfigurationInstance()
I want to show same AlertDialog on Home Page (MainActivity),if I navigate through it. I want to do this for all my pages to home one.
Anybody having any kind of idea & suggestion.
Thanks,
Upvotes: 0
Views: 2877
Reputation: 2721
Try this..
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
final Dialog CloseDialog = new Dialog(Sample.this);
CloseDialog.setTitle("Alert");
CloseDialog.setContentView(R.layout.exit_dialog_box);
ExitButton = (Button) CloseDialog.findViewById(R.id.ExitButton);
CloseExitDilog = (Button) CloseDialog
.findViewById(R.id.CloseExitDialog);
ExitButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
....../////////
}
});
CloseExitDilog.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
///
}
});
CloseDialog.show();
}
Upvotes: 0
Reputation: 15358
and if you want to show alert dialog on your 2nd Activity itself,before launching MainActivity then use following concept,
@Override
public void onBackPressed() {
showAlertDialog(SecondActivity.this);
}
private void showAlertDialog(Context mCtx) {
// TODO Auto-generated method stub
AlertDialog.Builder builder = new AlertDialog.Builder(mCtx);
builder.setCancelable(false);
builder.setTitle("Title");
builder.setMessage("Message text !!");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
finish();
}
});
builder.create().show();
}
Upvotes: 0
Reputation: 25830
Step1: Put your SharedPreferences inside your Second Activity
SharedPreferences myPrefs = getSharedPreferences(
"myPrefs", 0);
SharedPreferences.Editor prefsEditor = myPrefs.edit();
prefsEditor.putString("ALERT","YES");
prefsEditor.commit();
Step2:
Inside OnCreate of your HomeActivity Write Below Code
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
// Getting ALERT from Sharedpreference
myPrefs = getSharedPreferences("myPrefs", 0);
ALERT = myPrefs.getString("ALERT", "NO");
if(ALERT.equals("YES"){
// Write your Code for Showing Alert Dialogue
}else{
setContentView(R.layout.login);
// Write rest og the Code for Your Home Acitivity
}
Here you will have to setcontentview for HomeActivity after Finishing your AlertDialogue.
Hope it will help you.
Upvotes: 1
Reputation: 109237
If you want to get user interaction before user leave 2nd Activity then You have to put AlertDialog in onStop()
oronDestroy()
of 2nd Activity or If you want to show AlertDialog after your 2nd Activity is closed and You are in Main Activity then you have to put AlertDialog in onActivityResult()
of Main Activity. (But for this you have to start 2nd Activity from MAin Activity using startActivityForResult()
)
Upvotes: 0