在onStart中启动活动的问题
我试图从onStart启动一个浮动活动,以便在初始活动开始时从用户权限中检索一些信息。我有以下几点:在onStart中启动活动的问题
@Override
public void onStart(){
super.onStart();
callProfileDialog();
}
而且callProfileDialog()
就是:
private void callProfileDialog(){
Intent i = new Intent(this, com.utility.ProfileDialog.class);
startActivityForResult(i, PROFDIALOG);
}
ProfileDialog.class
返回从一个输入框的字符串。如果返回的结果是RESULT_CANCELED
,那么我重新开始活动。
我遇到的问题是,当程序启动时,屏幕只是黑色。如果我点击返回按钮RESULT_CANCELED
返回,那么最初的活动显示以及浮动活动(因为它得到RESULT_CANCELED
时它自己召回)。为什么我无法通过从onStart()调用ProfileDialog.class
来获得活动显示?当我在onCreate()的末尾调用它时,我得到了相同的结果,这是我切换到使用onStart()的方式。谢谢您的帮助。
编辑:我也曾尝试以下操作:
@Override
public void onWindowFocusChanged(boolean hasFocus){
if(hasFocus)
callProfileDialog();
}
但是,这也不行。一旦我点击后退按钮,一切正常,但没有这样做,它全部是黑色的。
我认为这是因为你还没有有效的背景。尝试下列操作之一:
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
callProfileDialog();
}
或
@Override
public void onResume(){
super.onResume();
callProfileDialog();
}
您应该重写Activity.onActivityResult()
,并设置你是从一个孩子返回一个标志,只有推出新的活动,如果该标志是不正确的:
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();
}
});
}
}
我得到同样的问题。当我启动应用程序时,它挂在黑屏上。我仍然需要点击后退按钮才能返回RESULT_CANCELED以显示任何内容。 – Fizz 2010-04-17 18:07:10
两个活动(MainActivity和ProfileDialog)都调用'setContentView(R.layout.layout_resource_file);'?我一定是读了你最初的问题,我认为唯一的问题是它不断重新启动对话活动。 如果您刚刚获得黑屏,通常意味着您没有该活动中的任何内容(并且默认主题为.Dark)。 至于RESULT_CANCELED,你是否在你从用户检索到你想要的输入后调用ProfileDialog中的'setResult(RESULT_OK,data);'? ProfileDialog中有一个按钮,它会调用'finish()'? – Joe 2010-04-20 21:09:02
我已经用主流活动和ProfileDialog活动的更明确流程编辑了我的答案,以显示取回RESULT_OK所需的内容。 – Joe 2010-04-20 21:42:03
我有一个类似的问题,并通过覆盖onAttachedToWindow()来获得我想要的行为。
@Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
callProfileDialog();
}
谢谢,我会试试这个。 – Fizz 2010-06-12 01:17:57
第一个建议有和我目前一样的问题。第二个建议虽然不仅仅是黑色,但一旦我点击后退按钮,它也不断启动浮动活动。 – Fizz 2010-04-14 20:13:51