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 9216399
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:24:34+00:00 2026-06-18T02:24:34+00:00

I have a piece of code to delete an Item in database. I am

  • 0

I have a piece of code to delete an Item in database. I am calling the same code from two different activities. So to avoid code repetition, I want to shift the code to the 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 application object, I changed the function to public, gave it two parameters for input: Context and rowID. But in the onPostExecute method of AsyncTask I have to close the activity. In the activity, I did this by finish(). How do I do it in this context? I have attached the code in application object also.

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();
}
  • 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-06-18T02:24:35+00:00Added an answer on June 18, 2026 at 2:24 am

    I think that what you are trying to do is fundamentally not a good idea.

    Outside of the Activity code, there are no guarantees that the activity still exists – the memory manager may have cleaned it up, the user may have pressed Back etc.

    The final design decision is up to you but I advise you to consider if this is really necessary.

    A little redundancy is okay in my opinion if it leads to more program stability and reliability.

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

Sidebar

Related Questions

I have been using a specific piece of code to delete files from a
Here I have piece of code, showing example of how I want to update
I have this piece of code in a function. I want to print the
I have a piece of code where I add objects and delete another one
I have this piece of code // TR Fading when deleted $('.delete').live('click', function() {
In a new piece of code I have several different classes that refer to
I have this piece of code in a class (DataBase) and I'm getting Not
Here's what I want to do. I have the following piece of code: Private
I still need your help. I have this piece of code that doesn't want
I have a piece of code that 'updates' an entry in the database by

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.