swapnil
swapnil

Reputation: 11

activity and back button

on the ui i am having a back button displayed. there are three activities say A,B,C. Activity A is splash screen. Activity B is home screen. Activity C is edit screen. on edit screen only i am having a back button. when the user press that i open the activity B which also contains some sort of http request and response. i want that the previous activity from stack should get displayed? how can i achieve that? i am able to open previous activity on pressing the back button of device?

when i press back button on device there doesnt goes any http request? i want to achieve this behaviour when i press the ui back button.

Upvotes: 1

Views: 6278

Answers (3)

T_D
T_D

Reputation: 3301

The solution with onKeyDown might work but using onBackPressed is much easier in my opinion.

You can intercept the Back-key with following override:

@Override
public void onBackPressed()
{
   //logic here, for example an intent
   Intent intent = new Intent(this, ActivityB.class);    
   startActivity(intent);   
}

And for the other way around super.onBackPressed(); returns to previous Activity in history.

Upvotes: 0

Labeeb Panampullan
Labeeb Panampullan

Reputation: 34823

I think in your back button you may call the intent for activity B and your http request and response code is in onCreate function

But the back button on device will not call onCreate
There is Two solution for this

One as Macarse say, listen onKeyDown

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)  {
    if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
        Intent i = new Intent(ActivityC.this,ActivityB.class);    
            startActivity(i);   
        return true;
    }

    return super.onKeyDown(keyCode, event);
}

And the second method is to write you code on onStart of ActivityB

protected void onStart() {
        //http request and response code
    }

This onStart will call all the time this when ActivityB open

Hope this help you

Upvotes: 4

Macarse
Macarse

Reputation: 93133

First of all, it's not good to have a back button displayed in the ui. Every android device has a back button in it.

If you want to handle the back button in a different way, check this link.

Upvotes: 0

Related Questions