Android 单击按钮向上导航
Android navigating up on button click
我有两个隐藏标题栏的活动 Activity A 和 Activity B。我不想使用 NavUtils.navigateUpFromSameTask。单击按钮时,我可以从 Activity A 转到 Activity B。我需要在不使用 Intent 的情况下导航回 Activity A。
Activity_A.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Go to Activity B" />
</LinearLayout>
Activity_B.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Go Back to Activity A" />
</LinearLayout>
Activity_A.java
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Activity_B.class);
startActivity(intent);
}
});
Activity_B.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.Avtivity_B);
Button button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
--> Code to Navigate up to Activity A <--
}
});
}
我隐藏了标题栏,我不想使用 NavUtils 或 intent。
一种方法是调用 finish()
方法。在你的 Activity_B.java
:
@Override
public void onClick(View arg0) {
finish();
}
您还可以在按钮上调用 Activity 的 onBackPressed()
Click
button.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View arg0) {
onBackPressed();
}
});
我有两个隐藏标题栏的活动 Activity A 和 Activity B。我不想使用 NavUtils.navigateUpFromSameTask。单击按钮时,我可以从 Activity A 转到 Activity B。我需要在不使用 Intent 的情况下导航回 Activity A。
Activity_A.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Go to Activity B" />
</LinearLayout>
Activity_B.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Go Back to Activity A" />
</LinearLayout>
Activity_A.java
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Activity_B.class);
startActivity(intent);
}
});
Activity_B.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.Avtivity_B);
Button button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
--> Code to Navigate up to Activity A <--
}
});
}
我隐藏了标题栏,我不想使用 NavUtils 或 intent。
一种方法是调用 finish()
方法。在你的 Activity_B.java
:
@Override
public void onClick(View arg0) {
finish();
}
您还可以在按钮上调用 Activity 的 onBackPressed()
Click
button.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View arg0) {
onBackPressed();
}
});