0

I have starting screen,which is my first activity in my application.

This activivty contains only one button and when its clicked, it should navigate to another activity which should prompt user for name.

If user press back button before confirming name, it should navigate back to the previous activity.

But when user confirm name, main activity should pop up and foregone activities should be removed from application.

I was looking for a solution and find it in creating static activity object , but i saw it was not wise and it is bad for memory.

What is the best approach for doing what i explained?

AustinWBryan
  • 3,249
  • 3
  • 24
  • 42
  • Why don't you clean the Activity stack and start a new MainActivity like : https://stackoverflow.com/questions/3473168/clear-the-entire-history-stack-and-start-a-new-activity-on-android ? – Bubu Jun 22 '18 at 10:21

3 Answers3

0

Hello this might be helpful check it once...

implement this in your "UserNameActivity"

@Override
public void onBackPressed() {

    if (name.isconfirmed())
    {
        Intent intent=new Intent(this,MainActivity.class);
        startActivity(intent);
        finish();
    }
    else
    {
        super.onBackPressed();
    }

}
0

try this :

implement this code at which activity to clear other stack activity

Intent intent = new Intent(this, newActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
this.startActivity(intent);

I hope this is helpful for you

Prince Dholakiya
  • 3,255
  • 27
  • 43
0

First Activity should call second Activity using startActivityForResult(). In first Activity implement onActivityResult() as follows:

  • If the resultCode is RESULT_CANCELED then the user has clicked the BACK key, so you don't need to do anything

  • If the resultCode is RESULT_OK then the user entered his username and you can start the main Activity and call finish(). This will remove the first Activity from the stack.

The second Activity should call setResult(RESULT_OK) and then finish() when the user has confirmed their username. This will end the second Activity and return to the first Activity by calling onActivityResult().

NOTE: The second Activity can return data (like the entered username or whatever) to the first Activity as "extras" in an Intent that it passes to setResult().

David Wasser
  • 93,459
  • 16
  • 209
  • 274