0

I would like to ask for suggestions regarding an app that i am making...

In my application, if a user presses a particular button in screen 1, then a dialog will appear on screen 2 ONCE screen 2 is opened. Otherwise, if the button on screen 1 is not clicked then the dialog on screen 2 will not show.

I am not asking you guys to post chunks of code for an answer, but I am just asking for the steps/algorithm that I should follow in order to achieve my goal...thanks in advance!

BurninatorDor
  • 1,009
  • 5
  • 19
  • 42

5 Answers5

1

What you should do is pass a boolean using an Intent from your first Activity (an activity represents a screen), and then load it in the second Activity. There, you can read the value and either show or not show the dialog.

More information on passing data from one Activity to another using intents

Community
  • 1
  • 1
skynet
  • 9,898
  • 5
  • 43
  • 52
1

When you create the Intent to start activity2 set an extra parameter on it with

boolean showDialog=...
intent.putExtra("yourextraname", showDialog );

Then in activity2 you can use this:

boolean showDialog=getIntent().getBooleanExtra("yourextraname", false);
H9kDroid
  • 1,814
  • 15
  • 17
1

The screen 2 has to know if it has to display a dialog or not.

Consequently, the screen 1 has to send some data to the screen 2 ( maybe a message "display the dialog", or maybe the content of this dialog ).

If screen 2 receives data, display the dialog. Else, do nothing.

In Android :

  • If the button is pressed, save data in your Activity1.
  • When you want to display screen2, send an Intent with startActivity(). If data are saved, put them in the Intent before launching it ( see Intent.putExtra() doc )
  • in the onCreate of Activity2, get the intent with getIntent() ) and check if it contains data or not ( see Intent.hasExtra() doc )
  • if true, then display your AlertDialog
louiscoquio
  • 10,638
  • 3
  • 33
  • 51
0

Rather than pass the data between two activities using Intent Extras, just store it in a Model class responsible for managing all the data in your application (Ala MVC design patterns):

// screen 1 button click
...
@Override 
void OnClick(View v)
{
    Model.setScreen1Clicked(true);
}

...

// screen 2 loads
...
void onCreate(Bundle savedInstanceState)
{
    if( Model.screen1Clicked() )
    {
        showDialog();
    }
}
Thomson Comer
  • 3,919
  • 3
  • 30
  • 32
0

I think the most fail-safe and simple solution would be setting a flag using SharedPreferences

Code example (you should check it against the documentation):

// screen one
sharedPreferences.edit().putBoolean("showWindow", true).commit();

// screen two
if(sharedPreferences.getBoolean("showWindow", false)) {
    sharedPreferences.edit().putBoolean("showWindow", false).commit();
    // show Dialog
}
LambergaR
  • 2,433
  • 4
  • 24
  • 34