views:

222

answers:

3

Hello everyone. I'm trying to start a floating activity from onStart to retrieve some info from the user right when the initial activity begins. I have the following:

@Override
public void onStart(){
 super.onStart();
 callProfileDialog();
}

And callProfileDialog() is just:

private void callProfileDialog(){
 Intent i = new Intent(this, com.utility.ProfileDialog.class);
    startActivityForResult(i, PROFDIALOG);
}

ProfileDialog.class returns a String from an input box. If the result returned is RESULT_CANCELED then I restart the activity.

The problem I'm having is that when the program starts, the screen is just black. If I hit the Back button a RESULT_CANCELED is returned then the initial activity shows as well as the floating activity (since it recalled itself when it got a RESULT_CANCELED). Why can't I get the activities show by calling ProfileDialog.class from onStart()? I got the same result when I called it at the end of onCreate() which is way I switch over to use onStart(). Thanks for the help.

Edit: I have also tried the following:

@Override
public void onWindowFocusChanged(boolean hasFocus){
    if(hasFocus)
    callProfileDialog();
}

But this doesn't work either. It all works fine once I hit the back button but without doing that, it's all black.

A: 

I think it's because you don't have a valid context yet. Try one of these:

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    callProfileDialog();
}

or

@Override
public void onResume(){
    super.onResume();
    callProfileDialog();
}
CaseyB
The first suggestion has the same problem as I currently do. The second suggestion though not only goes black but once I hit the back button it also keeps launching the floating activity.
Fizz
A: 

You should override Activity.onActivityResult() and set a flag that you are returning from a child, and only launch your new activity if that flag is not true:

public class MyActivity extends Activity {
  boolean returningFromChild = false;
  @Override
  public void onActivityResult(int requestCode, int resultCode, Intent data) {
    returningFromChild = true;
  }

  @Override
  protected void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    // Use your main layout here
    setContentView(R.layout.main);
  }

  @Override
  protected void onResume() {
    super.onResume();
    if (!returningFromChild) {
      callProfileDialog();
    }
    returningFromChild = false;
  }
}

// ProfileDialog.java
public class ProfileDialog extends Activity {
  @Override
  protected void onCreate(Bundle icicle) {
    super.onCreate(icicle); 
    // Use your dialog layout here
    setContentView(R.layout.profile_dialog);

    // Use the id of your "OK" button here:
    Button btn = (Button) findViewById(R.id.btnSaveInput);

    btn.setOnClickListener(new View.OnClickListener {
      public void onClick(View v) {
        // XXX: Get / validate the user's input. Can add it to a new Intent object as an Extra, 
        // and use the setResult(RESULT_OK, intent); version:
        setResult(RESULT_OK);
        finish();
      }
    });
  }
}
Joe
I get the same issue. It hangs on a black screen when I start the app. I still need to hit the back button for it to return a RESULT_CANCELED to get anything to show.
Fizz
Are both activities (MainActivity, and ProfileDialog) calling `setContentView(R.layout.layout_resource_file);` ? I must have read your initial question wrong; I thought the only problem was that it was continuously re-launching the dialog activity.If you're just getting a black screen, that generally means you don't have any content in that activity (and the default theme is .Dark).As for RESULT_CANCELED, are you calling `setResult(RESULT_OK, data);` in ProfileDialog after you've retrieved the input you want from the user? And is there a button in ProfileDialog that will call `finish()`?
Joe
I've edited my answer with a more explicit flow of both the main activity, and your ProfileDialog activity, to show what is required to get back a RESULT_OK.
Joe
A: 

I had a similar problem and got the behavior I wanted by overriding onAttachedToWindow() instead.

@Override
public void onAttachedToWindow() {
    super.onAttachedToWindow();
    callProfileDialog();
}
Thanks, I'll try this.
Fizz