Android – can I combine my code into some kind of “global activity”?
•
Android
Is there any global activity on Android that I put my code in one activity that will affect all activities in my project? This happens to me because the same code is in keyevent.keycode_ Written in several activities such as back
For example, I use here:
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
try {
final Intent itnt_BackServices = new Intent(this,
BackServices.class);
AlertDialog.Builder alert@R_372_2419@ = new AlertDialog.Builder(this);
alert@R_372_2419@.setTitle("Touch signs");
alert@R_372_2419@.setMessage("Do you want to quit!");
alert@R_372_2419@.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
stopService(itnt_BackServices);
mPlayer.stop();
finish();
}
});
alert@R_372_2419@.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
}
});
alert@R_372_2419@.show();
} catch (Exception e) {
// TODO: handle exception
}
}
return false;
}
I copy and paste it in each activity. I prefer to use some kind of global activity
resolvent:
You can create a class that extends activity, and then extend customactivity to all activity classes, just 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;
}
}
You can now extend this class using activity to extend any class
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码