我有2个布局,我可以从布局1访问page1和page 2,并且可以从任何页面从布局1访问Layout 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>
这是示例图片
答案 0 :(得分:0)
问题是您有2个活动。您所说的page1和page2仅基于我在代码中看到的内容,即您在MainActivity中设置了不同的文本onClick。
现在,如果您从laayout2活动回来,则将调用MainActivity的onCreate并将设置xml文件中的默认文本。 (这里有一个链接,用于了解活动生命周期https://developer.android.com/guide/components/activities/activity-lifecycle)
对于您的问题,我看到2种可能的解决方案。
解决方案1: 如果您在进行laayout2活动之前已在MainActivity中更改了文本,则可以将文本保存在savedInstanceState中。如果u返回并且onCreate中的saveInstanceState不为null,则可以将文本设置为之前的文本。那么此解决方案的问题将是没有办法回到文本的初始状态。 这是一个如何使用saveInstanceState How to use onSavedInstanceState example please
的示例解决方案2:
您无需为文本作常规更改,而是为第1页,第2页和第3页创建3个片段,而不是您的laayout2活动。现在,您在MainActivitys内容视图(通常为FrameLayout)中填充主片段(在您的情况下为page1),然后从那里通过片段事务打开其他片段。在那里,您将拥有一个backstack,它的onBackPressed会自动将您带回到之前打开的片段。那就是我会做到的方式。 这里有一些有关片段和片段事务的有用链接。
https://developer.android.com/guide/components/fragments
Replacing a fragment with another fragment inside activity group
祝您好运,希望对您有所帮助。