Android,How to start a new Activity from a dialog?
I would like to Start a new activity from my custom dialog, I have a simple Button and I want t开发者_JS百科hat when I press the button a new activity will start.
I try with Start:
Intent intent=new Intent(arg0.getContext(),IdResult.class);
startActivityForResult(intent, 0);
but it doesn't work; how can I make this work?
Use this
public class CustomDialog extends Dialog implements OnClickListener {
Button okButton, cancelButton;
Activity mActivity;
public CustomDialog(Activity activity) {
super(activity);
mActivity = activity;
setContentView(R.layout.custom_dialog);
okButton = (Button) findViewById(R.id.button_ok);
okButton.setOnClickListener(this);
cancelButton = (Button) findViewById(R.id.button_cancel);
cancelButton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (v == cancelButton)
dismiss();
else {
Intent i = new Intent(mActivity, IdResult.class);
mActivity.startActivity(i);
}
}
}
just add the this code inside the custom dialog
Intent i = new Intent(MyActivity.this, ItemSelection.class);
mActivity.startActivity(i);
It seems that you start new activity from non-activity class.
Just add intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
You can read more in reference page
精彩评论