java - 如何使用恢复按钮进入上一个 Activity ?

标签 java android android-studio android-intent android-studio-2.0

我的应用程序工作正常,但我的目的是当我关闭应用程序然后再次运行它时,我的应用程序将在最后一个 Activity 中打开。我希望当我再次打开时,主要 Activity 会显示出来,如果我单击“恢复”,则最后一个 Activity 将打开。所以我有 4 个名为 Main Activity (my main)p1p2p3:

主要 Activity :

public class MainActivity extends Activity {



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_page);



        Button resume=(Button)findViewById(R.id.resume);
        Button next=(Button)findViewById(R.id.next);
        Button exit=(Button)findViewById(R.id.exit);

        resume.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                if (!isTaskRoot()
                        && getIntent().hasCategory(Intent.CATEGORY_LAUNCHER)
                        && getIntent().getAction() != null
                        && getIntent().getAction().equals(Intent.ACTION_MAIN)) {

                    finish();
                    return;
                }


            }
        });

        next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, p1.class);
                startActivity(intent);
          }
      });

        exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                moveTaskToBack(true);
                android.os.Process.killProcess(android.os.Process.myPid());
                System.exit(1);

            }
        });


    }
}

XML 布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:text="resume"
        android:layout_width="wrap_content"
        android:id="@+id/resume"
        android:layout_height="wrap_content" />

    <Button
        android:text="next"
        android:id="@+id/next"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/exit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="exit"/>
</LinearLayout>

p1:

public class p1 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.p1);

        Button next = (Button) findViewById(R.id.next);
        Button home=(Button)findViewById(R.id.home);

        next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(p1.this,p2.class);
                startActivity(intent);

            }
        });

        home.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(p1.this,MainActivity.class);
                startActivity(intent);

            }
        });

        }
    }

p1 XML 布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:text="next"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/next"/>
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="page 1"/>
    <Button
        android:text="go in main"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/home"/>

</LinearLayout>

和p2、p3类似p1

例如:当我在 p2 中时,然后单击进入 main 并在 main 中,当单击退出并重新运行我的应用程序时,我的应用程序在 p2 中打开,我希望当我重新运行应用程序主 Activity 时打开,如果我单击“恢复”,然后单击 p2来秀一下。

最佳答案

要在重新启动时打开主要 Activity ,您应该调用

private void FinishActivity(){
this.finish();
}

startActivity(intent); 之后的 onClick 事件中

为了恢复您的 Activity ,将当前 Activity 类存储为共享首选项的字符串

moveTaskToBack(true); 之前在代码中调用以下方法

    private void storeCurrentActivity(){ 
        SharedPreferences myPref =getSharedPreferences("APP_SHARED_PREF",            Context.MODE_PRIVATE); 
        SharedPreferences.Editor editor = myPref.edit(); 
        editor.clear(); 
        String packageName = this.getPackageName(); 
        String className = this.getClass().getSimpleName(); 
        editor.putString("lastactivity",packageName+"."+className); 
        editor.commit(); 
}

并将结果存储到共享首选项

当您返回应用程序时,只需检查 SharedPreference

在您的主要 Activity 中使用以下代码:setContentView(R.layout.main_page);

String lastActivity= getLastActivity();
try {
            Intent fooActivity = new Intent(this,Class.forName(lastActivity))
            startActivity(fooActivity)
    } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }

将此方法放入您的 MainActivity

   private String getLastActivity(){
                  SharedPreferences myPref = =   getSharedPreferences("APP_SHARED_PREF", Context.MODE_PRIVATE);
                  String lastActivity= myPref.getString("lastactivity","");

        }

这可能对你有帮助

此特定问题的解决方案

用下面的代码替换您的主页按钮操作

home.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(p1.this,MainActivity.class);
                startActivity(intent);
                storeCurrentActivity();
                this.finish();

            }
        });

上面的代码将存储您的 Activity ,即如果您在 Activity p1 中,它将把 p1 存储为最后一个 Activity

另请删除onResume操作

关于java - 如何使用恢复按钮进入上一个 Activity ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38609179/

相关文章:

android - 在 Gradle 中,如何在控制台/事件日志中打印出一条消息?

Android Studio 在截屏时卡住

android - 如何在 Android Studio 中播放音乐?

java - 我使用数组编译 java 彩票程序时出现 2 个错误

java - 创建一个 Maven 原型(prototype)以从一个类模板生成多个类

android - 如何在触摸位置附近显示自定义弹出窗口,就像我们使用 ContextMenu 时那样?

java - activitythread.performlaunchactivity(activitythread$activityclientrecord intent) 找不到源

java - for 循环计算字符串中重复的字符数,然后删除重复的字符

java - 在 Java 中,如何显式引用 `this` 类的静态成员?

Android:在 Release模式下登录后启动时应用程序崩溃