Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6625543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:47:14+00:00 2026-05-25T21:47:14+00:00

Is there any global activity on Android such that I put my code in

  • 0

Is there any global activity on Android such that I put my code in that one activity, and it affects all activities in my project? This occurs to me because the same code is written in multiple activities like KeyEvent.KEYCODE_BACK

For example here I use:

public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK)) {
            try {
                final Intent itnt_BackServices = new Intent(this,
                        BackServices.class);
                AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
                alertbox.setTitle("Touch signs");
                alertbox.setMessage("Do you want to quit!");
                alertbox.setPositiveButton("Yes",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface arg0, int arg1) {
                                stopService(itnt_BackServices);
                                mPlayer.stop();

                                finish();
                            }
                        });
                alertbox.setNegativeButton("No",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface arg0, int arg1) {
                            }
                        });
                alertbox.show();
            } catch (Exception e) {
                // TODO: handle exception
            }
        }
        return false;
    }

I copy and paste this in each activity, and I would rather use some kind of global activity.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T21:47:15+00:00Added an answer on May 25, 2026 at 9:47 pm

    You can create a class that extends Activity and then extend the CustomActivity to all the Activity Class like this.

    public abstract class CustomActivity extends Activity{
    
        public abstract void initComponents();  // you can create a abstract method
        public abstract void addListner();       // you can create a abstract method
    
        @Override
        public boolean onKeyDown(int keyCode, KeyEvent event) {
    
             if ((keyCode == KeyEvent.KEYCODE_BACK)) {
                  // your stuff here....
                }
            return true;
        }
    }
    

    Now you can extend this class where you want to extend any class with Activity.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In Ruby is there any method that lists all the global variables available at
Is there any way to use global int constants in Objective C that work
are there any libraries for Linux wrote with C++, that could register global hotkeys
Is there any fundamental difference in binding a service to an android.app.Activity vs binding
Is there any way to create global styles for all TextBoxes used in application?
Is there any function / global variable in PHP that returns the current state
Is there any global function that I can override when the view controller is
I have been storing a global variable that's needed across Activities in my Android
Is there any way to tell GCC to not initialize a particular global array
hello i would like to know if there is any dll or global hook

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.