0

I have seen this question: Disable back button in android (Please do not mark as duplicate for this.)

My query is this: I have twenty activities in a row. I want to disable the back button, so that the user can never come back to the activity he once crosses. Currently, how I do this is override onBackPressed() and remove the super.onBackPressed() call. This works fine.

I now need to add forty more activities, and it should have the same effect. Is there a method where I can just disable the back button for the entire application without having to code it up in each Activity?

manfcas
  • 1,933
  • 7
  • 28
  • 47
Anindya Dutta
  • 1,972
  • 2
  • 18
  • 33

2 Answers2

3

Create BaseActivity and extend every your Activity with this BaseActivity, and add onBackPressed() logic in BaseActivity.

Ex:

public class BaseActivity extends AppCompatActivity {

    // Add your onBackPressed() logic here
}

Your activity,

public class MyActivityA extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_activity);
    }
}
Muthukrishnan Rajendran
  • 11,122
  • 3
  • 31
  • 41
  • Is it good practice to create an Activity which does not 'do' anything, and does not show itself to the user? – Anindya Dutta Aug 06 '17 at 08:01
  • This is like AppCompatActivity, which extends FragmentActivity which extends soo on. In your layer, you are creating one thats it. You can add common thing which you are doing in your Activities. **Note : BaseActivity you dont need to add in Mainfest** – Muthukrishnan Rajendran Aug 06 '17 at 08:04
  • I think this is the cleanest way to do it. Using the basic OOP principle of inheritance. :) Thanks. – Anindya Dutta Aug 06 '17 at 17:58
0

U can achieve this by finishing the pervious activity while going to next activity.

Deepak Rana
  • 539
  • 4
  • 18
  • I cannot finish the previous activity because my app is called by another app which needs some data back. So I am using onActivityResult() to propagate back my data from the last activity of my app through the first activity of my app and into the app that called me. Also, that would mean having to write `finish()` in each Activity. I'm looking for something which can be done only once. – Anindya Dutta Aug 06 '17 at 07:57