简体   繁体   中英

Disabling back button on multiple activities (Android)

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?

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

您可以通过在转到下一个活动时完成上一个活动来实现此目的。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM