Search code examples
javaandroidfirebasefirebase-authenticationondestroy

Overview button switches activity


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


Solution

  • 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();
    
    }
    

    }