我在文档中读到应该避免使用finish() - 但我真的没有看到更好的替代方案......
我只是想知道,如果有更好,更清洁的方法来做到这一点......
非常感谢
这是我基本上需要做的事情:
MainActivity
-> Sub1Activity
-> MySMSActivity
-> send SMS
-> handle if SMS was sent or not
<- finish()
<- finish()
(skip Sub1Activity)
MainActivity
MainActivity中的代码:
this.myBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
doSub1Activity("xyz");
}
});
public void doSub1Activity() {
Intent i;
i = new Intent(this, SUB1_screen.class);
startActivity(i);
}
Sub1Activity中的代码:
this.myBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
doMySMSActivity("xxx");
finish(); //<- this is the 1. call to "finish" I am concerned about
}
public void doMySMSActivity() {
Intent i;
i = new Intent(this, MySMSActivity.class);
startActivity(i);
}
MySMSActivity中的代码:
private void sendSMS(String phoneNumber, String message)
{
String SENT = "SMS_SENT";
String DELIVERED = "SMS_DELIVERED";
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0,
new Intent(SENT), 0);
//---when the SMS has been sent---
bRSMS_has_been_sent = new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode())
{
case Activity.RESULT_OK:
...
break;
default:
break;
}
finish(); // another call to finish so the user continues with
// MainActivity after SMS has been sent...
}
};
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
}
答案 0 :(得分:1)
您不必多次致电finish()
。如果您需要返回主活动,只需使用带有以下标志的意图启动它。如果活动已经存在,那将重复使用该活动。
Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_SINGLE_TOP
答案 1 :(得分:0)
为什么你让短信发送自己的活动?它不与用户交互 - 只是从Sub1Activity
中删除您的待处理意图