Following is an onclick method called when a button is clicked. But the code doesn’t stop my application. It sends a message and then closes and then restarts again.. I have read the lifecycle model of an activity and fyi this is a single activity application. I also researched on stackoverflow, many of the post describing to have a return statement after finish() call so i have done that too but no results.
Where am i going wrong ???
public void onClick(View v)
{
String text = "Demo Message";
String number = <set via a particular code>;
if(number.equals(""))
{
Toast.makeText(this, "Please Enter or Select a Number", Toast.LENGTH_SHORT).show();
}
else
{
sms = SmsManager.getDefault();
PendingIntent sentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, Main.class), 0);
sms.sendTextMessage(number, null, text, sentIntent, null);
Toast.makeText(this, "Message Sent to : " + number, Toast.LENGTH_SHORT).show();
// Stop the Activity...
finish();
return;
}
}
Everything is quite simple. Your PendingIntent calls Main activity after sending sms. So the process is the following:
You can simply test this by inputting in the
onCreate()method log task, for instanceLog.d("MyApp", "onCreate()");and you’ll see that it is called after your sms is sent.