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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:47:44+00:00 2026-05-27T11:47:44+00:00

I am trying to use handle database with insert, update, and delete such as

  • 0

I am trying to use handle database with insert, update, and delete such as notepad.
I have problem with back key because when I press the back key, I don’t want to save any data. In normal case which presses the confirm button, it will be saved into sqlite and will be displayed on listview.
How can I make cancel event through back key or more button event?
I am trying to use onBackPressed, onPause, and onResume.
When I press the back key in edit page, onPause() is calling, but I do not want to use
saveState() when I pressed back key. How can I make it?
Could you give me some feedback?
Thanks.

@Override
protected void onPause() {
    Toast.makeText(this, "onPause", Toast.LENGTH_SHORT).show();
    super.onPause();
    saveState();

}

@Override
protected void onResume() {
    Toast.makeText(this, "onResume", Toast.LENGTH_SHORT).show();
    super.onResume();
    Resume_populateFields();
}

@Override 
public void onBackPressed() { 
    Toast.makeText(this, "onBackPressed", Toast.LENGTH_SHORT).show();
      super.onBackPressed();
      finish();    
}    

private void saveState() {
    String name = (String) nameEdit.getText().toString();
    String category = (String) categoryEdit.getText().toString();
    String expired_date = (String) expired_Date_Btn.getText().toString();
    Bitmap imageBitmap = ((BitmapDrawable) mImageView.getDrawable())
            .getBitmap();
    ByteArrayOutputStream imageByteStream = new ByteArrayOutputStream();
    imageBitmap.compress(Bitmap.CompressFormat.PNG, 100, imageByteStream);

    if (mRowId == null) {
        long id = mDbHelper.insertItem(category, name, expired_date,
                imageByteStream);

        if (id > 0) {
            mRowId = id;
        }
    } else {
        mDbHelper.updateItem(mRowId, category, name, expired_date,
                imageByteStream);
    }
}

    private void Resume_populateFields() {
    if (mRowId != null) {
        Cursor data = mDbHelper.fetchItem(mRowId);
        startManagingCursor(data);
        // load information from sqlite
        nameEdit.setText(data.getString(data
                .getColumnIndexOrThrow(FridgeDbAdapter.KEY_NAME)));
        categoryEdit.setText(data.getString(data
                .getColumnIndexOrThrow(FridgeDbAdapter.KEY_CATEGORY)));
        expired_Date_Btn.setText(data.getString(data
                .getColumnIndexOrThrow(FridgeDbAdapter.KEY_EXPIRED_DATE)));

    } else {
        // call display date when list is clicked
        expired_Date_Btn.setText(new StringBuilder().append(mDay)
                .append("/")
                // month is 0 based. Then add 1
                .append(mMonth + 1).append("/").append(mYear).append(" "));
    }

}
  • 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-27T11:47:44+00:00Added an answer on May 27, 2026 at 11:47 am

    Define a boolean flag isOnBackeyPressed; Than:

    @Override 
    public void onBackPressed() { 
        Toast.makeText(this, "onBackPressed", Toast.LENGTH_SHORT).show();
          super.onBackPressed();
          isOnBackeyPressed = true;
          finish();    
    }    
    
    
    @Override
    protected void onPause() {
        Toast.makeText(this, "onPause", Toast.LENGTH_SHORT).show();
        super.onPause();
        if (!isOnBackKeyPressed)
           saveState();
    
    }
    

    If your lifecycle is different, just adopt the idea.

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

Sidebar

Related Questions

I'm trying to use my own form of connection pooling to handle database connections
I'm trying to use a database I have in Access 2010 and trying to
i have use mysql database . my problem is that i run a query
I'm pretty new to Java ME and i'm trying to use Microlog to handle
Heya, I'm trying to use Mono's SIMD to handle coordinates(X,Y,Z) in my project, but
I'm trying to create a use-once HTTP server to handle a single callback and
Whenever I use the ORM deisgner in LinqToSql I have real problems trying to
ATM I am trying to learn how to efficiently use database inidices and would
I'm trying to use SQLite's C API to open a database and access some
I am trying to connect to a MYSQL database using VB.NET to handle some

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.