finish() 的替代方法
我在文档中读到,应该避免使用 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);
}
I read in the docs that one should avoid using finish() - but I don't really see a better alternative for this...
I just like to know, if there is a better, cleaner way to do this...
Many thanks
This is what I basically need to do:
MainActivity
-> Sub1Activity
-> MySMSActivity
-> send SMS
-> handle if SMS was sent or not
<- finish()
<- finish()
(skip Sub1Activity)
MainActivity
The code in 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);
}
The code in 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);
}
The code in 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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不必多次调用
finish()
。如果您需要返回主活动,只需使用带有以下标志的意图即可启动它。如果该活动已存在,则将重用该活动。Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_SINGLE_TOP
You don't have to call
finish()
multiple times. If you need to return to the main activity simply start it using an intent with the flags below. That will reuse the activity if it already exists.Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_SINGLE_TOP
为什么您将短信发送本身作为一个自己的活动?它不与用户交互 - 只需从 Sub1Activity 中取出待处理的意图
Why you make SMS sending itself an own activity? It does not interact with user - just shell out your pending intent from Sub1Activity