简体   繁体   中英

Navigating between activities

I'm developing an app. for my semester project.App. is simple true&false game.I did something about it but i choked up one point.My problem is that handling activities. Here is the quick view of the design. http://imgur.com/2Fhsrn8 I created all activities and my codes working correct.But there is an issue.When i'm on third or fourth page(activity) and press back button on my phone it returns the second page then i automatically have another chance to answer question.All i want is turn the first screen when back button is pressed and clear all the data such as assign score as 0. I'will be grateful for any kind of help.

Use

finish();

for your current activity when you start the new intent(eg when going from activity 3 to activity on your picture) and add this to your activity's 2-3 if you want to start again when you click back, otherwise it will close your app.

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
    if ((keyCode == KeyEvent.KEYCODE_BACK))
    {
        //implement code to start 1st activity here!
    }
    return super.onKeyDown(keyCode, event);
}

you should override onBackPressed on your third, and fourth activity ->

@Override
public void onBackPressed() {

 // here you should create intent to your firstActivity
 // and assign variables -> e.g. 
 // score = 0;

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM