简体   繁体   中英

How to open alertDialog on click of Button in Next Activity from First Activity

I have Activity A and Activity B with one button Each. I would like to Open an Alert Dialog in Activity B when user press Button in A and Similarly in From Activity B when user press Button, Again alert Dialog should appear in Activity A.

I could either use Shared prefrences. .. and could override the onStart method to check the value and show alert dialog

@Override
public void onStart(){
super.onStart();
//Alert Dialog here
}

but if there is any other way..I can implement this ?

Note: User can also navigate from menu between Activity A and B so I don't want to show any alert dialog when they use menu to navigate between two activites. But only when they press the Button.

In Activity A :

Intent k = new Intent(this, ActivityB.class);
k.putExtra("shouldStartAlertDialog", true);
startActivity(k);

Then in Activity B, in OnCreate method :

if(getIntent().getBooleanExtra("shouldStartAlertDialog", false)){
   //Show alertdialog
}

You can set bundle in intent extras

private void startActivity(boolean showDialog,Class activity){
        Bundle bundle=new Bundle();
        bundle.putBoolean("show_dialog",showDialog);
        Intent intent=new Intent(context, activity);
        intent.putExtras(bundle);
        context.startActivity(intent);
    }

Now call start activity like this

startActivity(true,ActivityB.class)

in you ActivityB's onCreate

Bundle extras = getIntent().getExtras();
boolean showDialog = extras.getBoolean("show_dialog",false);
if(showDialog)  // code to show dialog

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM