0

I am trying to implement block functionality in my app. There is only one activity that allows the user to block others. However this activity can be accessed using 4 different flow.

Use Case:

When a user is blocked the back stack needs to be cleared and the user should be redirected to the screen that started the flow.

Since I don't know which flow was used by the user to reach the profile screen, I cannot explicitly launch any activity using Intent.

Please suggest a way.

Edit

Adding navigation flows.

Flow 1:

Homescreen -> Tab 1 (Contains List of elements) -> Element Details Screen -> Block Screen.

Flow 2:

Homescreen -> Tab 1 (Contains List of elements) -> Element Details Screen -> Mutual Connection Screen -> Block Screen.

Flow 3:

Homescreen -> List of Connections Screen -> Connection Details Screen -> Block Screen.

Flow 4:

HomeScreen -> Tab 3 -> Find from friends -> Block Screen

Sufian
  • 6,405
  • 16
  • 66
  • 120
Rajas47Ashtikar
  • 583
  • 2
  • 6
  • 11

3 Answers3

1

Update

In your various "root" activities, probably in onResume() update a SharedPreferences store with some value that will identify that the most recent root activity the user saw was this one.

@Override
protected void onResume() {
    super.onResume();

    SharedPreferences preferences = getSharedPreferences("NAME_PREFERENCES", Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = preferences.edit();
    editor.putString("KEY_ACTIVITY", someIdentifyingKey);
    editor.apply();
}

In your "block" activity, when it's time to close the activity, read this value.

private void goBackToRoot() {
    SharedPreferences preferences = getSharedPreferences("NAME_PREFERENCES", Context.MODE_PRIVATE);
    String identifier = preferences.getString("KEY_ACTIVITY", null);
    Class<? extends Activity> rootActivity = /* choose the correct activity class based on `identifier` */;

    Intent intent = new Intent(this, rootActivity);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    startActivity(intent);
}

If you simply call finish() on your activity, the user will be returned to whatever the previous activity was. No need to create an Intent just to go back.

Ben P.
  • 52,661
  • 6
  • 95
  • 123
  • No i have intermediate screens and i need to finish them all. So just finish shall not be ok. – Rajas47Ashtikar Jul 21 '17 at 15:00
  • One way could be to start each activity in the chain using `startActivityForResult()` instead of just `startActivity()`. The "block" screen can return a result when the user blocks someone, and all the activities in the chain can receive that result, set a result of their own, and close themselves until you're back to the root you want. Another way could be to record somehow (e.g. shared preferences) which root the chain starts at, and then start that root (with SINGLE_TOP) from the "block" activity. – Ben P. Jul 21 '17 at 15:17
  • @Rajas47Ashtikar the solution by Ben is great. Ben, can you add a little bit of code just to give him an overview of the solution? – Sufian Jul 21 '17 at 15:22
  • @Rajas47Ashtikar I've updated my answer with an example of how to use `SharedPreferences` to achieve your desired behavior. – Ben P. Jul 21 '17 at 15:41
  • @BenP. this will work fine but I liked your suggestion of `startActivityForResult()` better. But also solution with `Intent` will be fine, although most complex of the three. – Sufian Jul 21 '17 at 16:09
  • @Sufian that one might be a little more "pure" but it's a maintenance nightmare (I know because I've worked on apps where we had to close multiple activities when the user logs out from deep in the hierarchy). – Ben P. Jul 21 '17 at 16:12
  • I have created a central broadcast receiver. Whenever block is completed successfully a broadcast is sent to the CentralBroadcastReceiver, the receiver then sends out a broadcast of action type blocked, The activities and fragments are registered to this receiver and then on their respective screen they handle the broadcast the way they want to, for eg. on some screen i want an automatic server call so some activities shall perform a network call, some activities shall finish them selves – Rajas47Ashtikar Jul 22 '17 at 09:26
0

I think on you Block Screen you need to clear all the back stack and relaunch the Home Screen. the simple way to achieve this is to use Intent.

Intent intent = new Intent(context, HomeScreen.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK
                | Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
((Activity) context).finish();
Randhir Kumar
  • 197
  • 10
0

I have created a central broadcast receiver. Whenever block is completed successfully a broadcast is sent to the CentralBroadcastReceiver, the receiver then sends out a broadcast of action type blocked, The activities and fragments are registered to this receiver and then on their respective screen they handle the broadcast the way they want to, for eg. on some screen i want an automatic server call so some activities shall perform a network call, some activities shall finish themselves

Rajas47Ashtikar
  • 583
  • 2
  • 6
  • 11