Android按下按钮即可导航

时间:2015-05-14 16:46:14

标签: android navigation

我有两个活动活动A和活动B,隐藏了标题栏。我不想使用NavUtils.navigateUpFromSameTask。我可以点击按钮从活动A转到活动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或意图。

2 个答案:

答案 0 :(得分:1)

这样做的一种方法就是调用finish()方法。在Activity_B.java

@Override
    public void onClick(View arg0) {
        finish();
    }

答案 1 :(得分:0)

您还可以在按钮上调用活动的onBackPressed()

button.setOnClickListener(new View.OnClickListener(){
    @Override
    public void onClick(View arg0) {
          onBackPressed();
     }
});