如何停止使用Android的意图的活动? [英] How to stop an activity in android using intent?

查看:159
本文介绍了如何停止使用Android的意图的活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想这是一个基本的问题。有没有什么方法可以用有意停止活动。

I think this is a basic question. Is there any option to stop an activity by using intent.

Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:5554"));
startActivity(intent);

这是我的code。我想制止这种行为(这意味着,我要放弃这一调用),如果用户正忙什么的。我该怎么办是什么?我想这样的:

This is my code. I would like to stop this activity (That means, i want to drop this call) if the user is busy or something. What can I do for that? I tried this:

if (condition) {
    Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:5554"));
    startActivity(intent);
}else {
    this.finish();
}

没有用的,但。没有任何人有一个建议?

But of no use. Does anybody have a suggestion?

推荐答案

我前几天有这个问题,我很高兴地告诉你,我已经找到一种方法解决这个问题。

I had this problem a few days ago, and I'm happy to tell you I've found a way around this.

首先,该活动要停止在添加此的Andr​​oidManifest.xml

First of all, to the activity you want to stop add this in the AndroidManifest.xml:

android:launchMode="singleTop"

我要使用复选框的例子。当它的检查活动开始时未选中将杀死的活动。

I'm going to use a CheckBox example. When it's checked the activity is started and when unchecked will kill the activity.

例活动A被调用的Activity B,然后使用意图杀死它。

Example Activity A is calling Activity B and then killing it using an intent.

code被放在一个:

Code to be put in A:

checkbox.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            Intent intent = new Intent(A.this, B.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
            if (enable.isChecked()) {
                intent.putExtra("keep", true);
                startActivity(intent);
            }
            else
            {
                intent.putExtra("keep", false);
                startActivity(intent);
            }
        }
    });

code付诸B:

Code to be put into B:

boolean keep;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.B);
    intent = this.getIntent();
    boolean keep = intent.getExtras().getBoolean("keep");
    if(keep==true)
    {
        //execute your code here

    }
 }
    @Override
protected void onNewIntent(Intent intent)
{
    super.onNewIntent(intent);
    keep = intent.getExtras().getBoolean("keep");
    if(keep==false)
    {
        B.this.finish();
    }
}

说明:这是什么做主要是,当复选框被选中它调用的活动,并传递一个布尔值,如果这是真的的活动更让并带到前台。现在,如果你不及格的标志 singleTop 那么这个活动的许多实例将被创建。 singleTop 确保只有相同的实例被调用。现在,当复选框被选中为保持新的值传递这是B.验证如果未选中,活动A将被路过假的,也就是B从withing自行终止的 onNewIntent()的功能。

Explanation : What this basically does is, when the checkbox is checked it calls the activity and passes a boolean value, if it's true the activity is kept alive and is brought to the foreground. Now, if you don't pass the flag singleTop then many instances of this activity will be created. singleTop makes sure only the same instance is called. Now, when the checkbox is unchecked a new value for keep is passed which is verified in B. If unchecked, the Activity A will be passing false, and hence B terminates itself from withing the onNewIntent() function.

希望帮助!

PS - 您可以从另一个活动结束b活动了。只需使用 如果其他活动是C:

P.S - You can close Activity B from another Activity too. Just use If the other activity is C:

Intent intent = new Intent(C.this, B.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.putExtra("keep", false);
startActivity(intent);

这篇关于如何停止使用Android的意图的活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆