user1446632
user1446632

Reputation: 427

Reloading activities in android

I am making an android application where i want the activity to reload itself once the back button is pressed on another activities. I have a set of activities. So i got the main one, and a lot of other ones. Let's say the main one is activity-1, and the other ones are activity-2,3,4,5,6,7. Once the user presses a button that takes him to activity-2, or 3, or 4, he will be presented with some buttons again, where one of them will take him to activity-3, or 4, or 5. So when the user is at one activity launched from the main activity, and presses the back button, the main activity should be reloaded, how is this possible? Please help and thanks so much in advance!

Upvotes: 2

Views: 1021

Answers (5)

user3363081
user3363081

Reputation: 21

While starting the Activity call the finish the current Activity for example.

    Intent intent = new Intent(getActivity(), AudioRecordingActivity.class);
    startActivity(intent);
    getActivity().finish();

Then onbackPressed call the new Activity For eg:.

@Override
public void onBackPressed() {
    Intent intent = new Intent(getApplicationContext(), IndexPageActivity.class);
    startActivity(intent);
    super.onBackPressed();
}

Upvotes: 2

rizzz86
rizzz86

Reputation: 3990

Override the onBackPressed() method of your Activity (activity-2, 3, 4 ...) and write code for calling the Main activity in it.

For Example:

@Override
public void onBackPressed() {

    Intent mainIntent = new Intent(context, YourMainActivity.class);
    context.startActivity(mainIntent);

    return;
}   

This will reload your main activity when you press back button from any of your activity.

Upvotes: 0

VenomVendor
VenomVendor

Reputation: 15382

use finish(); this sends back to the previous activity in your case activity-1 without reloading.

Upvotes: 0

Imran Rana
Imran Rana

Reputation: 11899

Try:

@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
                Intent intent = new Intent(this,name of the class where you want to go to);

                startActivity(intent);

                return true;
        }
        return super.onKeyDown(keyCode, event);
    }

this code captures the event on back button click and takes you to your desired activity.

Upvotes: 0

bluebyte
bluebyte

Reputation: 560

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(null);
}

Maybe you should try this in main activity

Upvotes: 0

Related Questions