vijay
vijay

Reputation: 46

Android navigating up on button click

I have two activities Activity A and Activity B with title bar hidden. I don't want to use NavUtils.navigateUpFromSameTask. I can go from Activity A to Activity B on button click. I need to navigate back to Activity A without using Intent.

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 <--   
                }
       });
  }

I have title bar hidden and i don't want to use NavUtils or intent.

Upvotes: 1

Views: 1281

Answers (2)

Bidhan
Bidhan

Reputation: 10697

One way of doing this is just calling the finish() method. In your Activity_B.java:

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

Upvotes: 2

theapache64
theapache64

Reputation: 11754

You could also Call onBackPressed() of the Activity on the buttonClick

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

Upvotes: 0

Related Questions