finish() 的替代方法

发布于 2024-12-14 03:51:02 字数 2143 浏览 1 评论 0原文

我在文档中读到,应该避免使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

逆流 2024-12-21 03:51:02

您不必多次调用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

东走西顾 2024-12-21 03:51:02

为什么您将短信发送本身作为一个自己的活动?它不与用户交互 - 只需从 Sub1Activity 中取出待处理的意图

Why you make SMS sending itself an own activity? It does not interact with user - just shell out your pending intent from Sub1Activity

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文