-2

Someone ask me a question a week ago and till now i did not get the answer of that question.The question was how we can go back to previous Activity by pressing back button of device without using intent or finish().

Suraj Makhija
  • 1,376
  • 8
  • 16
DkThakur
  • 516
  • 2
  • 5
  • 17
  • I remember seeing a question a week ago about this. saying the back button press does not work to switch activities. I guess the overall solution was to use fragments instead since it is quite easier to reinflate. good luck i am following this as well – koksalb Mar 29 '17 at 11:51
  • 3
    Its normal activity stack, just make sure you are not overriding onBackPressed() method in activity. If you are following this, you doesn't need to use intent or finish to switch between activity stack. – Keyur Thumar Mar 29 '17 at 11:57
  • when you press back button of device it doesnt call onBackpressed(),may be internally it is calling finish – Janardhan R Mar 29 '17 at 11:58
  • Keyur Thumar can you explain it in simple manner – DkThakur Mar 29 '17 at 11:58

6 Answers6

4

If You Are Using Intent Than Make Sure You don't Call finish(); After Using

StartActivity(IntentObj); 

Method Than As per activity stack backbutton will do its work and go to previous Activity.

And Also Make Sure That You Are Not overriding onBackPressed() method in activity.

OR

You Can Set Home Button For go to Specific Activity, For That You Have to Give Back button into Action bar. For That in onCreate()

getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);

And in manifest.xml

<activity
        android:name="com.example.myfirstapp.DisplayMessageActivity"
        android:label="@string/title_activity_display_message"
        android:parentActivityName="com.example.myfirstapp.MainActivity" >
        <!-- Parent activity meta-data to support 4.0 and lower -->
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.myfirstapp.MainActivity" />
    </activity>
3

You Can Try this

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

after that go to Manifest and add this line under activity tag

 android:noHistory="false"
2

There is no need to do any code for return to previous activity by on pressing back button of device, android system maintain stack for activity and do that job for you.

If you want add back button in action bar then do it.

Add below code in onCreate method

getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);

And in Menifest.xml file inside your activity add below code

<meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.myfirstapp.MainActivity" />

In android:value add your parent activity to which you want to redirect.

For more reference visit below link...

https://developer.android.com/training/implementing-navigation/ancestral.html

Jaimin Prajapati
  • 341
  • 5
  • 14
1

if you have a button like this in your current activity, then if you click on that button you can go to the previous activity.Note that current activity will be finish by this.

button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            finish();    //finish current activity and go back to previous Activity
        }
    });
0

Using getActivity().onBackPressed(); in the onclick event of a button in a fragment works for for me.

-2

You can do it by simply overriding onBackPressed method in your activity.

  @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

Hope this help!

Akp
  • 259
  • 1
  • 9