mathius1
mathius1

Reputation: 1391

Android app Back button shows blank screen

I have an android app that starts with a simple screen and a "start" a button. Once the user clicks the button it retrieves code from a url and puts it into a list. If you press back from the listview it will show a blank screen not a screen with the "start" button. All the code is in a onCreate statement. Please excuse me if I am missing the obvious this is my first real android app (aside from the Hello Android tutorial). Below is the code in the first activity if that helps.

public class Main extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Button next = (Button) findViewById(R.id.butQR);
    next.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            setContentView(R.layout.listplaceholder);
            Intent myIntent = new Intent(view.getContext(), NewListActivity.class);
            myIntent.putExtra("race_id", view.getId());
            myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
            myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(myIntent);
        }

    });
}

Update: I wa using setContentView(R.layout.listplaceholder); on button click and in the new oncreate for the next activity. Thanks for the help.

Upvotes: 3

Views: 4619

Answers (2)

Alabhya
Alabhya

Reputation: 490

From what you have mentioned it doesn't seem like you need the intent flags. When you set the FLAG_ACTIVITY_CLEAR_TOP flag, if the activity to be launched is already running, all other activities on top of it are closed such that the activity being called (which was already running and in the stack) is now on top. Now, when you press the back button, it should go to the activity that was running before this, but since your Main activity was closed because of that flag, you get a blank screen.

Upvotes: 1

K-ballo
K-ballo

Reputation: 81349

You shouldn't be creating a new task for a subactivity, and I see no reason to clear the top of the task stack. If you don't want the start activity to show when the user goes back, you can simply finish() it after starting the new activity.

Upvotes: 2

Related Questions