简体   繁体   中英

Activity doesn't close when back button is pressed

I have an application that uses crosswalk. I use it in two activities.

  1. In this activity, I have a crosswalk view that shows a list of selectable items and when selected launch another activity.
  2. In this new activity I open another crosswalk view that runs the selected item from the previous activity.

The issue that I am having is when in the second activity when I press the back button it goes back to a black screen. If I press the back button again, it then closes the activity.

What can I do to close the activity instead of going back to the black screen? This doesn't happen on all items just a few, and with those few I think that a page redirect is happening in crosswalk so when I press back it just goes to the previous screen.

Here is the activity:

package com.gamesmart.gamesmart;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import org.xwalk.core.XWalkPreferences;
import org.xwalk.core.XWalkView;

public class Play extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_play);

        Intent intent = getIntent();
        String url = intent.getStringExtra("url");

        XWalkView xWalkWebView = (XWalkView)findViewById(R.id.xwalkWebViewPlay);

        // Turn on debugging if we are in DEBUG mode
        if (BuildConfig.DEBUG) {
            XWalkPreferences.setValue(XWalkPreferences.REMOTE_DEBUGGING, true);
        }

        // Load the url
        xWalkWebView.load(url, null);
    }

    @Override
    public void onBackPressed(){
        finish();
    }
}

I don't think that my onBackPressed is doing what it should be...

You forgot

super.onBackPressed();

Just use below code

@Override
public void onBackPressed() {
    super.onBackPressed();
}

Try this

 public void onBackPressed(){
        Intent a = new Intent(Intent.ACTION_MAIN);
        a.addCategory(Intent.CATEGORY_HOME);
        a.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(a);

    }

Maybe you should try to call the super method in your onBackPressed() , or if you want to go back to the previous activity, to bring to front the other activity in your onBackPressed() , and then, finish your Activity like this :

public void onBackPressed() {
    Intent main = new Intent(this, YourClass.class);
    main.putFlags(Intent.ACTIVITY_BROUGHT_TO_FRONT); // Or something like this, I dont really remember the flag name
    startActivity(main);
    super.onBackPressed();
}

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