2

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...

Get Off My Lawn
  • 34,175
  • 38
  • 176
  • 338

4 Answers4

4

You forgot

super.onBackPressed();

Just use below code

@Override
public void onBackPressed() {
    super.onBackPressed();
}
N J
  • 27,217
  • 13
  • 76
  • 96
2

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);

    }
John Joe
  • 12,412
  • 16
  • 70
  • 135
0

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();
}
Mesabloo
  • 337
  • 4
  • 13
0

Just write this command when you want to exit :

overridePendingTransition ( 0 , 0 );
super.finish ( );
super.onStop ( );
super.onDestroy ( );
overridePendingTransition ( 0 , 0 );
Tyler2P
  • 2,324
  • 26
  • 22
  • 31
Harsh
  • 1
  • 1