How do I call the finish () action method from the Application object?
I have a piece of code to delete an item in a database. I am calling the same code from two different activities. Therefore, to avoid repetition of code, I want to move the code into an application object. The code in one of the activities looks like this:
private void deleteItem() {
AlertDialog.Builder alert = new AlertDialog.Builder(Activity1.this);
alert.setTitle(R.string.confirmTitle);
alert.setMessage(R.string.confirmMessage);
alert.setPositiveButton(R.string.delete_btn,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int button) {
final DbHelper db = new DbHelper(Activity1.this);
AsyncTask<Long, Void, Object> deleteTask = new AsyncTask<Long, Void, Object>() {
@Override
protected Object doInBackground(Long... params) {
db.deleteItem(params[0]);
return null;
}
@Override
protected void onPostExecute(Object result) {
finish();
}
};
deleteTask.execute(new Long[] { rowID });
}
});
alert.setNegativeButton(R.string.cancel_btn, null).show();
}
Now, to put it in the application object, I changed the function to public, gave it two parameters to input: Context and rowID. But in the onPostExecute AsyncTask method, I need to close the activity. In this operation, I did it using finish (). How can this be done in this context? I also added code to the application object.
public void deleteItem(final Context context, final long rowID) {
AlertDialog.Builder alert = new AlertDialog.Builder(context);
alert.setTitle(R.string.confirmTitle);
alert.setMessage(R.string.confirmMessage);
alert.setPositiveButton(R.string.delete_btn,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int button) {
final DbHelper db = new DbHelper(context);
AsyncTask<Long, Void, Object> deleteTask = new AsyncTask<Long, Void, Object>() {
@Override
protected Object doInBackground(Long... params) {
db.deleteItem(params[0]);
return null;
}
@Override
protected void onPostExecute(Object result) {
finish();
}
};
deleteTask.execute(new Long[] { rowID });
}
});
alert.setNegativeButton(R.string.cancel_btn, null).show();
}
I think that what you are trying to do is fundamentally not a good idea.
Outside of the action code, there is no guarantee that the activity still exists - the memory manager could clear it, the user could press Back, etc.
The final design decision is up to you, but I advise you to consider whether this is really necessary.
A little redundancy, in my opinion, is good if it leads to more stability and reliability of the program.
Instead of moving it to the app, create a BaseActivity class (which extends the Activity class), all your activities extend BaseActivity .. and the generic code will be hosted in BaseActivity
Context
can be added to Activity
:
Activity activity = (Activity) context;
And than just using:
activity.finish();
Simple, add an instance of the activity as well
public void deleteItem(final Context context, Activity activity,final long rowID){
activity.finish();
}
Cannot be called Application.finish()
like in C #. You can use a method like this:
Activityname.finish();
This is a good decision. Hope I helped.