2

i have certain screens which allow user to comment , reply and view certain other features . Even the Navigation drawer is designed to get itself updated once the user login to the application .

My problem is i dont know about any good approach how to refresh the whole activity stack once the user Logins and a session is created for him/her .

Some screens shows comment edittext boxes , these boxes are only visible if the user log in, Is their any way so that, if the user tries to login from any screen and gets back after complete login session the comment box appears quick and the drawer also gets updated automatically .

Do i have to make some listener and call refresh on each activity in the stack or to kill all back-stack and re direct user to a new main activity and their after.

Can any one please get me know of some good approach i must use !

Peter
  • 1,069
  • 2
  • 13
  • 24
  • A -> B -> C -> Login in some case A -> D -> F -> Login in other case A -> E -> G -> Login For now am Clear all back Stack and move the User with new A What i want is User After login Sets back to C / F / G and refresh all activity which uses the Session to create some Screen , To be more clear assume that Activity B is to show user profile pic after login, i want to refresh all stack activity which uses session details and refresh it after login rather then just deleting all back stack and moving to a new A What will be the best approach ! – Peter May 15 '17 at 08:52

4 Answers4

2

I would broadcast an intent with your own custom action when the user logs in then listen for this event in an activities\fragments which are interested though a BroadcastReceiver https://developer.android.com/reference/android/content/BroadcastReceiver.html

For broadcasting the login intent: https://developer.android.com/reference/android/content/Context.html#sendBroadcast(android.content.Intent)

You'll then need to refresh any UI elements when the activities resume. I guess you could kill all activities on login but seems like a poor user experience.

Pete Hendry
  • 291
  • 2
  • 7
  • Please see my comment in top i have added , may be the picture will get more clear – Peter May 15 '17 at 08:53
  • I think Changing the backstack when the user logs in would lead to a very confusing user experience - if I'm reading your intentions correctly. I would stick with returning to acitvity A – Pete Hendry May 15 '17 at 12:29
  • Yeah, am doing the same for right now ! Let me know if theirs any more of information you could suggest any time. Thankx man for the help . – Peter May 16 '17 at 07:26
0

Try to use

finish();
startActivity(getIntent());
Sasi
  • 445
  • 4
  • 19
0

You can just re-launch the activity after a successful login. Or, if you are using a separate a different activity for user login, then you can launch the particular activity in whichever you want to get re-directed to. And, that activity will be re-initialized.

If you are worried about persistence factor of user data or, some other user state at that particular moment, you can use saveInstanceState and then sync from it after re-initializing the activity. But, this time the activity will(may) contain some of the logged-in user's information like profile pic, name, gender -etc.

  • Option 1

For refreshing the current activity, you can use this below code snippet-

public void refreshActivity (View v){
    Intent intent = getIntent();
    finish();
    startActivity(intent);
}
  • Option 2

You can save your current activity state-

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
  super.onSaveInstanceState(savedInstanceState);
  // Save UI state changes to the savedInstanceState.
  // This bundle will be passed to onCreate if the process is
  // killed and restarted.
  savedInstanceState.putBoolean("MyBoolean", true);
  savedInstanceState.putDouble("myDouble", 1.9);
  savedInstanceState.putInt("MyInt", 1);
  savedInstanceState.putString("MyString", "Welcome back to Android");
  // etc.
}

So, you can save your instance state by putting some information as key-value pair relevant to your activity. Then revert it back from the same saved data.

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
  super.onRestoreInstanceState(savedInstanceState);
  // Restore UI state from the savedInstanceState.
  // This bundle has also been passed to onCreate.
  boolean myBoolean = savedInstanceState.getBoolean("MyBoolean");
  double myDouble = savedInstanceState.getDouble("myDouble");
  int myInt = savedInstanceState.getInt("MyInt");
  String myString = savedInstanceState.getString("MyString");
}

I am putting the original answer's link for your reference. Hope this solves the issue for you.

Community
  • 1
  • 1
Sudip
  • 647
  • 2
  • 7
  • 28
  • Please see my comment in top i have added , may be the picture will get more clear – Peter May 15 '17 at 08:53
  • I understood your requirement. You not only want to refresh the state of the current activity but instead of going back to main activity(A), you want to update the state of all the activity in your activity call stack. Well, I don't know the exact solution to this. But, there is a way in which you can update the current activity and make sure that after pressing the **back** button, you get back to the **main activity** and it's get refreshed. please go to this [link](http://www.tutorialspoint.com/android/android_session_management.htm) if you want to know more on this. – Sudip May 15 '17 at 09:44
  • Okey , i went through the link i am already using the session Concept ! Thanks for the efforts! Need to figure some better move ! Thanx man – Peter May 16 '17 at 07:24
0

You can use this.recreate() on restart, like this:

 @Override
protected void onRestart() {
    this.recreate();
    super.onRestart();
}
סטנלי גרונן
  • 2,917
  • 23
  • 46
  • 68
Rajnish Sharma
  • 390
  • 2
  • 14