0

I'm developing an Android Webview app at the moment. I'm using the following code to exit my app. The problem is that by using this code, I can't return to the previous page anymore so it's not yet what I had in mind.

Goal:

I'd like to show the below toast message only when pressing the back-button on the homepage. When the user is on a different page than the homepage, pressing the back-button should just return to the previous page.

How could I do something like that?

private Boolean exit = false;
@Override
    public void onBackPressed() {
        if (exit)
            this.finish();
        else {
            Toast.makeText(this, "Press again to close the app.",
                    Toast.LENGTH_SHORT).show();
            exit = true;
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    exit = false;
                }
            }, 3 * 1000);

        }

    }
Stan
  • 937
  • 5
  • 15
  • 33

2 Answers2

0

You have to have some kind of variable for knowing in which page are you.

private Boolean exit = false;
@Override
public void onBackPressed() {
     if(page != "homepage"){
          super.onBackPressed(); // Calls the Overriden Method 
     }
    else
    {
    if (exit)
        this.finish();
    else {
        Toast.makeText(this, "Press again to close the app.",
                Toast.LENGTH_SHORT).show();
        exit = true;
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                exit = false;
            }
        }, 3 * 1000);

    }
  }

}
PaytoN
  • 154
  • 10
  • Thanks for the reply! I had something like that in mind, but my real problem is that I don't exactly get how to define that variable? Let's say my website is called http://example.com , how should I define the page variable? Because right now I'm getting the error "page cannot be resolved to a variable" (which is normal). – Stan Nov 30 '14 at 19:01
  • You can split your URL ( http://yoururl.com/index.php ) and taking the name of 'index.php' or '' – PaytoN Nov 30 '14 at 19:48
0

If you are working with activities then try to declare their parent activities in AndroidManifest.XML
It will also create the backButton on the actionBar.

  <activity
        android:name=".MyActivity"
        android:label="@string/app_name"
        android:parentActivityName="com.projectname.homepage">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.projectname.homepage" />
    </activity>

In your Homepage declare this code onBackPressed.

@Override
public void onBackPressed() {
    if (back_pressed + 2000 > System.currentTimeMillis()) {
        Intent intent = new Intent();
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        super.onBackPressed();
    } else
    Toast.makeText(this, "Press once again to exit!",
            Toast.LENGTH_SHORT).show();
    back_pressed = System.currentTimeMillis();
}
Hanan
  • 444
  • 4
  • 16