Android : android intent button with alert dialog

on Wednesday, October 29, 2014


I am working on simple android app where user can go to another activity after clicking a button on alert dialog:


enter image description here


I tried but it always make my app crash.


here is my code:



public class MainActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);



AlertDialog.Builder renameDialog = new AlertDialog.Builder(MainActivity.this);
renameDialog.setTitle("alert");


renameDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {

public void onClick(DialogInterface arg0, int arg1) {
launchIntent();
}
});

renameDialog.show();
}

private void launchIntent() {
Intent it = new Intent(MainActivity.this, BActivity.class);
it.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(it);
}
}

0 comments:

Post a Comment