0

I have 2 activities Activity A and Activity B. My app starts in Activity A and I have a button on Activity A that leads to Activity B. When I'm in Activity B, I press the Overview Button(The square button) and select my app. When my app loads up, Activity B is destroyed and it loads up into Activity A instead. What is going on and how do I get it to save my settings and load into Activity B?

By extension i'm having an app that loads into firebase authentication and then operates on a firebase database. How do I save the state of it when the overview button is pressed? Do I need to save anything with firebase authetication or do I just save all the data I need to access and manage the firebase database with that?

I watched a couple videos on it where they are talking about screen rotations and saving states, but there's little information about the overview button.

Thanks, Reinaldo

Reinaldo
  • 25
  • 4

1 Answers1

0

What about saving the current activity to a value in shared preferences and then checking the value in the onCreate method of activity A. You can have a switch statement that redirects to whichever activity the user was on before.

activity A would look like this:

public class A extends AppCompatActivity {

private String currentActivity;
private final String CURRENT_ACT = "current activity";
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    SharedPreferences sharedPreferences = getSharedPreferences("shared preferences", MODE_PRIVATE);
    currentActivity = sharedPreferences.getString(CURRENT_ACT, "A");

    switch (currentActivity){
        case "A":
            break;

        case "B":
            Intent intent = new Intent(this, B.class);
            startActivity(intent);
            break;
    }

}

}

and B would look like this:

public class B extends AppCompatActivity {

private String currentActivity;
private final String CURRENT_ACT = "current activity";

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    currentActivity = "B";

    SharedPreferences sharedPreferences = getSharedPreferences("shared preferences", MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putString(CURRENT_ACT,currentActivity);
    editor.commit();

}

}

Trevor Soare
  • 120
  • 8
  • What would I need to save? I am worried that after activity B has it's onDestroy method, activity A has it's on create method. I have savedpreferences which will be accessed in Activity A. If I'm in activity B now, and this time I close the app. Activity B is destroyed via ondestroy method and saves data because the app is closing. Now I open the app, It loads Activity A, which calls it's oncreate method which uses the saved preferences to go back to activity B. This is not supposed to happen. – Reinaldo Sep 16 '20 at 00:56
  • Edited with an example of what i mean – Trevor Soare Sep 16 '20 at 01:17
  • Alright, it works! Thanks! I have another question, I have an Activity A that runs fragment 1 and 2. Where do I place the shared preferences if I want to restore the fragments? Thanks – Reinaldo Sep 17 '20 at 00:20
  • In the oncreate of each fragment or activity that you have, save a new value to shared preferences like you did with activity b. for example save C in fragment 1 and D in fragment 2. then add more cases to the switch statement in activity a to redirect the same way you did with activity b – Trevor Soare Sep 17 '20 at 00:34
  • thanks, that works. What if I have Activity A, with fragment 1 and 2 and fragment 1 has a recycler view I want to save the position of it? How do I do that? Code would be appreciated. Thanks – Reinaldo Sep 17 '20 at 17:43
  • If I write anymore code for you I'm going to want credit for this app ;). Save the position to shared preferences then in your switch statement for that Fragment you'll have to put the position in a bundle and set it as the arguments for that fragment. Lots of information on that here https://stackoverflow.com/questions/12739909/send-data-from-activity-to-fragment-in-android – Trevor Soare Sep 17 '20 at 18:31
  • :D ok, thanks! I think I figured it out. I used this: protected void onBindViewHolder(@NonNull MerchantViewHolder holder, int position, @NonNull final Merchants model) { lastposition =holder.getAbsoluteAdapterPosition(); SharedPreferences sharedPreferences = getActivity().getSharedPreferences("root_preferences", getActivity().MODE_PRIVATE); SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putInt("lastPos", lastposition); editor.commit(); } – Reinaldo Sep 17 '20 at 19:33
  • SharedPreferences sharedPreferences = getActivity().getSharedPreferences("root_preferences", getActivity().MODE_PRIVATE); recyclerView.scrollToPosition(sharedPreferences.getInt("lastPos", 0)); – Reinaldo Sep 17 '20 at 19:33