java - 从 BroadcastReceiver 转到上一个 Activity ?

标签 java android

我需要关闭来自广播接收器的当前 Activity 。我不确定如何从中调用完成,也许有一种方法可以模拟“后退”按键。只要能完成工作,任何实现都可以。

@Override
public void onReceive(Context context, Intent intent) {
// How can I finish the current activity here?
}

最佳答案

在你的广播接收器上写: YourCurrentActivityName.this.finish();

或者你可以用this.finish();终止前面的activity;所以卡住的最后一个打开出现在前面。


更新: 第一种情况的代码:

使用广播接收器在后台堆栈终止 Activity :

public class ActivityFirstName extends Activity {

    private BroadcastReceiver mFinishReceiver;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // other code

        if (mFinishReceiver == null) {
            IntentFilter intentFilter = new IntentFilter();
            intentFilter.addAction("com.example.ACTION_TERMINATE");// a string to identify your action
            mFinishReceiver = new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {
                    // How can I finish the current activity here?
                    if ("com.example.ACTION_TERMINATE".equals(intent.getAction())) {
                        ActivityFirstName.this.finish();
                    }
                }
            };
            registerReceiver(mFinishReceiver, intentFilter);
        }

        // other code

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (isFinishing()) {
            if (mFinishReceiver != null) {
                unregisterReceiver(mFinishReceiver);
            }
        }
    }

}

以及前面/当前运行的activity,广播的发送者:

public class ActivitySecondName extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second);

        // code code code

        final Button button = (Button) findViewById(R.id.button_id);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Perform action on click
                terminateBackActivities();
            }
        });
    }

    private void terminateBackActivities() {
        Intent i = new Intent("com.example.ACTION_TERMINATE"); // the two action strings MUST be same
        // i.putExtra(...); // to send extra data
        sendBroadcast(i);
    }

}

关于java - 从 BroadcastReceiver 转到上一个 Activity ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14639835/

相关文章:

android - 无法让 clipChildren=false 属性起作用

android - 数据绑定(bind)、TabLayout 和 “No view found for id for fragment”

java - 在 IntelliJ IDEA 13 中运行编译的 Groovy 代码时出现运行时错误

java - 登录后执行 ConstraintSet 后按钮不可见

java - 用户无法注销 firebase android

java - 让 Cassandra 使用备用 Java 安装

Android 对话框关闭而不是取消

android - 生成签名 APK 时出现 "Expected resource of type interpolator [ResourceType]"和 "Cannot resolve symbol"错误

java - Android Activity ListView 高亮案例

java - SonarQube 8.2 分析显示 0 代码覆盖率