我有两个活动a和title bar隐藏的活动B。我不想使用navutils.navigateupfromsametask。我可以从"活动"到活动b上的按钮单击。我需要在不使用意图的情况下导航回活动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或意图。
这样做的一种方法只是调用finish()
方法。在您的Activity_B.java
中:
@Override
public void onClick(View arg0) {
finish();
}
您也可以在buttonClick上调用活动的onBackPressed()
button.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View arg0) {
onBackPressed();
}
});