【问题标题】:How to make back button resume back parent activity?如何使返回按钮恢复返回父活动?
【发布时间】:2019-08-07 15:34:59
【问题描述】:

我有 2 个布局,我可以从布局 1 访问第 1 页和第 2 页,并且可以通过任何页面从布局 1 访问布局 2。 现在的问题是当我访问布局 2 表单第 2 页(即再见)然后我按后退按钮时我返回第 1 页(即 Hellow World)而不是第 2 页,我如何返回第 2 页而不是第 2 页1?

这是我的Mainactivity 函数

  public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    Button next,layout;
    TextView string;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        next = findViewById(R.id.button);
        layout = findViewById(R.id.button2);
        next.setOnClickListener(this);
        layout.setOnClickListener(this);
        string = findViewById(R.id.textView);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button: {  string.setText("Good bye");
            break;}

            case R.id.button2 :{
                Intent stat = new Intent(this, laayout2.class);
                startActivity(stat);
            }
        }

    }
}

这里是布局 2 java

        public class laayout2 extends AppCompatActivity {

            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_laayout2);
            }
            @Override
            public boolean onOptionsItemSelected(MenuItem item) {
                switch (item.getItemId()) {
                    // Respond to the action bar's Up/Home button
                    case android.R.id.home:
                        Toast.makeText(getApplicationContext(),"Back button clicked", Toast.LENGTH_SHORT).show();
                        //main.startProcess(1);
                        break;
                }


         return super.onOptionsItemSelected(item);
        }
    }

这里是activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:text="Next"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:text="to Layout2"
        app:layout_constraintBottom_toTopOf="@+id/textView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

这是示例图片

【问题讨论】:

  • 你能贴一些代码吗?例如意图/交易。
  • 我没有得到我应该发布的代码??
  • 在您的帖子中,我们看不到您在做什么。例如,如果您打开片段,则可以将解决方案添加到后台堆栈。但是,如果没有最少的相关代码,它就无法为您提供帮助。
  • 我做了一些修改,希望对您有所帮助

标签: android


【解决方案1】:

问题是您有 2 个活动。您所称的 page1 和 page2 是基于我在您的代码中看到的,只是您在 MainActivity 中设置了不同的 onClick 文本。

现在,如果您从您的 laayout2 活动返回,则调用 MainActivity 的 onCreate 并将设置您的 xml 文件中的默认文本。 (这里是了解activity生命周期的链接https://developer.android.com/guide/components/activities/activity-lifecycle

对于您的问题,我看到了 2 个可能的解决方案。

解决方案 1: 如果您在进入 laayout2 活动之前更改了 MainActivity 中的文本,您可以将文本保存在 savedInstanceState 中。如果您回来并且 onCreate 中的 savedInstanceState 不为空,您可以将文本设置为之前的内容。这个解决方案的问题将是没有办法回到文本的初始状态。 这里举例说明如何使用 saveInstanceState How to use onSavedInstanceState example please

解决方案 2:

您无需更改文本,而是为第 1 页、第 2 页和 1 个片段制作 3 个片段,而不是您的 laayout2 活动。现在,您在 MainActivitys 内容视图(通常是 FrameLayout)中填充主片段(在您的情况下为 page1),然后通过片段事务从那里打开其他片段。在那里,您将有一个 backstack,onBackPressed 会自动将您引导回之前打开的片段。这就是我会做的方式。 这里有一些关于片段和片段交易的有用链接。

https://developer.android.com/guide/components/fragments

Replacing a fragment with another fragment inside activity group

祝你好运,希望对你有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-31
    • 1970-01-01
    • 2013-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多