概述按钮切换活动

huangapple go评论65阅读模式
英文:

Overview button switches activity

问题

我有两个活动,Activity A 和 Activity B。我的应用程序从 Activity A 开始,在 Activity A 上我有一个按钮,可以跳转到 Activity B。当我在 Activity B 时,我按下概述按钮(方形按钮),然后选择我的应用程序。当我的应用程序加载完毕时,Activity B 被销毁,然后加载到 Activity A,而不是 Activity B。发生了什么,我该如何使其保存我的设置并加载到 Activity B?

顺带一提,我有一个应用程序,它首先加载到 Firebase 认证,然后对 Firebase 数据库进行操作。当按下概述按钮时,如何保存其状态?我是否需要保存任何与 Firebase 认证有关的内容,还是只需保存需要访问并管理 Firebase 数据库的所有数据?

我看了一些关于屏幕旋转和状态保存的视频,但很少提到概述按钮。

谢谢,
Reinaldo

英文:

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

答案1

得分: 0

关于将当前活动保存到共享首选项中的值,然后在活动A的onCreate方法中检查该值。您可以使用switch语句重定向到用户之前所在的任何活动。

活动A的代码如下:

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;
    }

}
}

活动B的代码如下:

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

}

}
英文:

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

}

}

huangapple
  • 本文由 发表于 2020年9月16日 07:40:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/63911199.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定