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

  • Home
  • SEARCH
  • 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 7857099
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T20:55:39+00:00 2026-06-02T20:55:39+00:00

This is making me a bit crazy. I have the following Application: Activity A

  • 0

This is making me a bit crazy.

I have the following Application:

Activity A -> Activity B -> Activity C

A is defined in the manifest as android:launchMode=”singleTask”

B starts C as follows:

        Intent startActivity = new Intent();
        startActivity.setClass(this,C.class);
        startActivity.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        startActivity(startActivity);

C talks back and forth with a remote system over a socket.

While C is communicating, the Back key is disabled. Here is C’s onKeyDown():

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {

    if (sessionActive() && (keyCode == KeyEvent.KEYCODE_BACK)) {

        return false;
    }
    else if (!sessionActive() && (keyCode == KeyEvent.KEYCODE_BACK)){

            Log.d("C","done talking to socket, got Back key, exit Activity");
        /* delete data from an internal static object */
    }

    return super.onKeyDown(keyCode, event);
}

sessionActive() returns true if we are still talking over the socket and we want the Back key disabled.

When sessionActive() is false we are done talking and we WANT the Back key to exit Activity C.

The problem is, its not working. I’ve followed this via log messages and the 1st time sessionActivity() == false I do get the “done talking to socket” message so I KNOW I’m getting to the right place. But, I have to press Back a 2nd time in order to get Activity C to go away.

Any ideas?

UPDATE UPDATE:

I have tried both answers and have gotten the same confusing results.

Here is a bit of my logcat showing the execution of the diffent lifecycle methods. I have a number of questions interspersed in the logcat text.

C.onCreate() 
C.onStart() 
C.onResume() 
C.onKeyDown() sessionActive()==false, keyCode = BACK              
                          this should get us out but doesn't

C.finish() sessionActive is FALSE                                
                          shouldn't this go to onStop()?

C.onPause() sessionActive is FALSE
C.onRestart() sessionActive is FALSE                              <-  ???????? onRestart?
C.onStart() sessionActive is FALSE                                <-  ???????? onStart?
C.onResume() sessionActive is FALSE
C.onStop() sessionActive is FALSE                                 <- this makes sense
C.onDestroy() sessionActive is FALSE                           <- so does this
C.onPause() sessionActive is FALSE                             <- ????????
C.onResume() sessionActive is FALSE

C.onKeyDown() sessionActive is FALSE, keyCode = BACK
                        this is 2nd back press that does exit
C.finish() sessionActive is FALSE
C.onPause() sessionActive is FALSE                there is no onRestart here!!!!!!!!!
C.onStop() sessionActive is FALSE
C.onDestroy() sessionActive is FALSE

I don’t understand the flow of control. I would have thought finish() sent control to onDestroy() and we’d be done.

  • 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-02T20:55:40+00:00Added an answer on June 2, 2026 at 8:55 pm

    You can try:

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        if ( keyCode == KeyEvent.KEYCODE_BACK )
        {
            if ( sessionActive() )
                return false;
    
            Log.d("C","done talking to socket, got Back key, exit Activity");
            /* delete data from an internal static object */
            finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This problem is making me a bit crazy. I have something like this <h3>Feeds
So, I have this bit of wild, crazy code, that is making the compiler
I'm making this game where I'm trying to pair people. So I have this
I need help with making this bit of code faster: UnitBase* Formation::operator[](ushort offset) {
I'm having a bit of a brain fart on making this code more concise(preferably
I'm making this website, where I want to have a quote shown on the
I have a 64-bit-only application that I am developing in Objective-C on Cocoa. It
It is looking like i have a major memory leak in this little bit
I am running into an issue and it is making me a bit crazy.
I'm having a bit of trouble making this JQuery work in Internet Explorer, it

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.