0

I have created an 'Enter PIN' screen. This pin activity will be called in onStart() of activity A like this -

@Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
        Log.d("pin", "on start 2");
        if (MainActivity.flag) {
            Intent intent = new Intent(con, PinActivity.class);
            MainActivity.flag = false;
            startActivity(intent);

        }
    }

However, I only want this activity to show up when user comes from background to foreground. Not when it comes from activity B (activity B comes after activity A).

This logic relies on this MainActivity.flag 's value, So I set it

  1. true in onStop() so that pin screen show up while coming to foreground. and,
  2. have put flag = false in onBackPressed() of activity B, so that pin screen doesn't show up while coming from B to A.

But there is flaw in the logic, because pin screen shows up while coming from B to A, while it should not. (PS - Please ask me to clarify if you don't understand my question.)

Darpan
  • 5,623
  • 3
  • 48
  • 80

2 Answers2

0

You can pass extra data from B to A to clarify that it is from B to A, not from background.
And then in A, your condition of showing Pin Activity should check if extra data exists.

Intent theintent = new Intent(A.this,B.java);
theintent.putExtra("fromB",true);
startActivity(theintent);

and check in Activity A:

Intent i= getIntent();
i.getExtra("fromB");
jjLin
  • 3,281
  • 8
  • 32
  • 55
0

The first thing you should ask yourself is: Why do you wan't to display pin activity only when comming from background?

Wouldn't it be interesting to store the pin for the user so that he doesn't have to retype it everytime ? You could store it using SharedPreference and redirect the user to this screen only if you don't have any stored pin. You can then add a button for your user if he wants to modify the pin manualy.

SharedPreference example

Edit:

If you really have to stick with this workflow, you can to something like this:

@Override
protected void onCreate(Bundle savedInstanceState)
{
    ...

    if (savedInstanceState == null)
    {
        Intent intent = new Intent(this, PinActivity.class);
        startActivity(intent);
    }
}

Checking savedInstanceState ensure that this will be called only the first time the activity is created and should fit to your workflow.

Community
  • 1
  • 1
user2641570
  • 804
  • 8
  • 20
  • You see this is not appropriate answer to my question. I have a business case where I don't want to store pin. This answer deserved a downvote, but I thought of putting a comment instead. – Darpan Jul 03 '14 at 06:45
  • Right my bad. Can you tell me more about your workflow. When do you go from B to A? Only with back or do you also call startactivity from B to A. – user2641570 Jul 03 '14 at 08:51