[Android Q&A, Help & Troubleshooting] asliyanage:
Note: Text content in the code blocks is automatically word-wrapped

package com.connection.phonedetails;

import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.widget.Toast;

public class Test extends Activity {

private static final int REQUEST_CODE = 10;

[MENTION=439709]override[/MENTION]

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

//setContentView(R.layout.alert);

DatabaseHandler db = new DatabaseHandler(this);

Intent intent = new Intent(this,AlertDialogExample.class);

startActivityForResult(intent,REQUEST_CODE);

}

[MENTION=439709]override[/MENTION]

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

if (requestCode == 10) {

if(resultCode == RESULT_OK){

String result=data.getStringExtra("TYPE");

Toast.makeText(getApplicationContext(),"Your sim type iss POST paidddddddddd", Toast.LENGTH_LONG).show();

}

if (resultCode == RESULT_CANCELED) {

Toast.makeText(getApplicationContext(),"Your sim type iss PREEEEE paidddddddddd", Toast.LENGTH_LONG).show();

}

}

}

}

this is alert dialog activity.

view plaincopy to clipboardprint?

Note: Text content in the code blocks is automatically word-wrapped

package com.connection.phonedetails;

import android.app.Activity;

import android.app.AlertDialog;

import android.content.Context;

import android.content.DialogInterface;

import android.content.Intent;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.Toast;

public class AlertDialogExample extends Activity {

// this context will use when we work with Alert Dialog

final Context context = this;

// just for test, when we click this button, we will see the alert dialog.

private Button button;

[MENTION=439709]override[/MENTION]

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

//setContentView(R.layout.alert);

/* Alert Dialog Code Start*/

AlertDialog.Builder alert = new AlertDialog.Builder(context);

alert.setTitle("Select Your SIM Type"); //Set Alert dialog title here

//alert.setMessage("Enter your Name Here"); //Message here

alert.setPositiveButton("Post Paid", new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int whichButton) {

// String value = input.getText().toString();

// Do something with value!

//You will get input data in this variable.

Toast.makeText(getApplicationContext(),"Post Paid", Toast.LENGTH_LONG).show();

//db.addContact(new SimDetails( telephonyManager.getSimSerialNumber(), telephonyManager.getNetworkOperatorName(),"Post"));

Intent returnIntent = new Intent();

returnIntent.putExtra("TYPE","POST Paid");

setResult(RESULT_OK,returnIntent);

finish();

}

});

alert.setNegativeButton("Pre Paid", new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int whichButton) {

// Canceled.

Toast.makeText(getApplicationContext(),"Pre Paid", Toast.LENGTH_LONG).show();

Intent returnIntent = new Intent();

returnIntent.putExtra("TYPE","PRE Paid");

setResult(RESULT_CANCELED,returnIntent);

finish();

}

});

AlertDialog alertDialog = alert.create();

alertDialog.show();

/* Alert Dialog Code End*/

}

/*

[MENTION=439709]override[/MENTION]

public void finish() {

Intent data = new Intent();

// Return some hard-coded values

data.putExtra("TYPE", "Swinging on a star. ");

data.putExtra("returnKey2", "You could be better then you are. ");

setResult(RESULT_OK, data);

super.finish();

}*/

}

this is working fine.Anyway if added the below two line end Oncreate method of the main activity it doesn't wait untill the alert dialog finishes.it goes to contact activity .So i don't have chance to select option from alert dialog.how to avoid this situation?

view plaincopy to clipboardprint?

Note: Text content in the code blocks is automatically word-wrapped

Intent contact = new Intent(getApplicationContext(),Contacts.class);

startActivity(contact);