Android Home 活动启动子活动

发布于 2024-11-16 14:52:48 字数 2458 浏览 2 评论 0原文

我对 Android 编程比较陌生。我在工作中接到一项任务,需要创建一个自定义主页活动启动器。我做了一些研究,并在 Android 开发者网站 (home example< /a>)。这是我的原型的开始。

自定义主页活动需要要求用户进行身份验证(输入一些简单的凭据)。我的想法是从“主页”活动启动一个子活动,并将意图中的结果传回“主页”活动。我需要能够捕获有关这些凭据的信息,并且该信息将被传递回家庭活动。但是,我在尝试这个时遇到了问题。我在 LogCat 中收到一条日志,内容如下:“活动正在作为新任务启动,因此正在取消活动结果。”

我知道 startActivityForResult 方法,但这似乎对我不起作用。这是我从 Home 活动启动活动的位置:

@Override
protected void onResume() {
    super.onResume();
    bindRecents();

    Intent iii = new Intent(this, Login.class);
    startActivityForResult(iii, STATIC_LOGIN_INTEGER_VALUE);
}

当该代码执行时,我从 ActivityManager 标记获取上述日志。

我的登录活动有一个按钮,用户输入凭据后将点击该按钮。如果凭据良好,那么我尝试执行以下操作(我放入了多个日志,以便我可以尝试弄清楚发生了什么):

public void onClick(View src) {
    // check for authentic credentials
    if(IsValid())
    {
        Intent loginAuth = new Intent("Login");
        loginAuth.putExtra("userRole", userRole);

        Log.d("LOGIN", "Setting result...");
        if (getParent() == null) {
            Log.d("LOGIN", "Parent was null");
            setResult(Activity.RESULT_OK, loginAuth);
        }
        else {
            Log.d("LOGIN", "setting result on parent...");
            getParent().setResult(Activity.RESULT_OK, loginAuth);
        }

        Log.d("LOGIN", "Finishing the activity");
        finish();
    }
}

在清单文件中将这些活动定义如下:

    <activity android:name="Home"
            android:theme="@style/Theme"
            android:launchMode="singleInstance"
            android:stateNotNeeded="true">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.HOME"/>
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

    <activity android:name="Login"
              android:label="Login"
              android:launchMode="singleInstance">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.HOME" />
        </intent-filter>
    </activity>

我 登录活动的意图过滤器。我最初将其设置为空。我的 launchMode 也为空白(我认为默认为标准)。我已经尝试过大多数这些选项,但似乎没有什么可以改变这样一个事实:ActivityManager 似乎想要将活动作为新任务启动,并且想要忽略返回的意图(我需要)。

I am relatively new to Android programming. I have been given a task at work where I need to create a Custom Home activity launcher. I did a bit of research and found the example on the Android developer website (home sample). This was the start for my prototype.

The custom Home activity will need to require the user to authenticate (enter some simple credentials). My thought was to launch a sub-activity from the Home activity and pass back the results in the intent to the Home activity. I need to be able to capture information about these credentials and that information was going to be passed back to the Home activity. However, I have problems when trying this. I get a log in the LogCat that says the following: "Activity is launching as a new task, so canceling activity result."

I am aware of the startActivityForResult method, but that does not seem to be working for me. Here is where I launch the activity from my Home activity:

@Override
protected void onResume() {
    super.onResume();
    bindRecents();

    Intent iii = new Intent(this, Login.class);
    startActivityForResult(iii, STATIC_LOGIN_INTEGER_VALUE);
}

When that code executes, I get the above mentioned log from the ActivityManager tag.

My Login activity has a button that the user will hit once they have entered their credentials. If the credentials are good, then I try to do the following (I put in several logs so that I could try to figure out what is going on):

public void onClick(View src) {
    // check for authentic credentials
    if(IsValid())
    {
        Intent loginAuth = new Intent("Login");
        loginAuth.putExtra("userRole", userRole);

        Log.d("LOGIN", "Setting result...");
        if (getParent() == null) {
            Log.d("LOGIN", "Parent was null");
            setResult(Activity.RESULT_OK, loginAuth);
        }
        else {
            Log.d("LOGIN", "setting result on parent...");
            getParent().setResult(Activity.RESULT_OK, loginAuth);
        }

        Log.d("LOGIN", "Finishing the activity");
        finish();
    }
}

I defined these activities in my manifest file as the following:

    <activity android:name="Home"
            android:theme="@style/Theme"
            android:launchMode="singleInstance"
            android:stateNotNeeded="true">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.HOME"/>
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

    <activity android:name="Login"
              android:label="Login"
              android:launchMode="singleInstance">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.HOME" />
        </intent-filter>
    </activity>

I was playing around with the intent filter on the Login activity. I had originally had it set to nothing. I also had the launchMode blank (which defaults to standard I believe). I have played around with most of these options and nothing seems to change the fact that the ActivityManager seems to want to launch the activity as a new task and wants to ignore the returned intent (which I need).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

二智少女 2024-11-23 14:52:48

问题是您使用 launchMode="singleInstance" 声明了您的活动,因此 Android 在启动登录活动时会创建一个新任务(即新进程)。不同任务中的活动不能互相返回结果。来自 Activity.startActivityForResult() 文档:

例如,如果您正在进行的活动
启动使用 singleTask 启动
模式,它不会在您的任务中运行并且
因此您将立即收到
取消结果。

singleInstance 与 singleTask 类似,但限制更严格。尝试从清单中删除 launchMode 属性,以便登录活动将在同一任务中启动,然后在需要在单独的任务中启动不同的活动时使用 FLAG_ACTIVITY_NEW_TASK。

巴里

The problem is that you declared your activities with launchMode="singleInstance", so Android creates a new task (i.e. a new process) when it launches the Login activity. Activities in different tasks cannot return results to each other. From the Activity.startActivityForResult() docs:

For example, if the activity you are
launching uses the singleTask launch
mode, it will not run in your task and
thus you will immediately receive a
cancel result.

singleInstance is like singleTask but even more restrictive. Try removing the launchMode attribute from your manifest so that the Login activity will launch in the same task, then using FLAG_ACTIVITY_NEW_TASK when you need to launch a different activity in a separate task.

Barry

梦魇绽荼蘼 2024-11-23 14:52:48

文档说

例如,如果您要启动的活动使用 singleTask
启动模式,它不会在您的任务中运行,因此您将
立即收到取消结果。

这是错误的

使用singleTask就可以了,不能使用singleInstance

让我们回到问题本身。问题是什么?

问题是,当您在 Activity 上调用 startActivityForResult() 时,调用者 Activity 和您要启动的 Activity 必须位于同一任务上,startActivityForResult() 才能正常工作正确地,这意味着您无法使用Intent.FLAG_ACTIVITY_NEW_TASK对活动调用startActivityForResult()。

为什么 singleInstance 不起作用?

因为“singleInstance”活动是其任务中唯一的活动。如果它启动另一个活动,则该活动将被启动到不同的任务中,无论其启动模式如何 - 就好像 FLAG_ACTIVITY_NEW_TASK 在意图中一样。在所有其他方面,“singleInstance”模式与“singleTask”相同。

为什么singleTask有效?

因为 singleTask 不需要为启动的新活动创建新任务。

输入图片这里的描述

我引用了这个答案解释两种启动模式之间的区别。

The documentation says

For example, if the activity you are launching uses the singleTask
launch mode, it will not run in your task and thus you will
immediately receive a cancel result.

THIS IS WRONG.

You can use the singleTask just fine, what you can't use is singleInstance.

Let's return to the problem. What is the problem?

The problem is that when you call startActivityForResult() on an activity, the caller activity and the activity you are launching must be on the same task for the startActivityForResult() to work properly that means that you can't call startActivityForResult() to an activity using the Intent.FLAG_ACTIVITY_NEW_TASK.

why singleInstance isn't working?

Because a "singleInstance" activity stands alone as the only activity in its task. If it starts another activity, that activity will be launched into a different task regardless of its launch mode — as if FLAG_ACTIVITY_NEW_TASK was in the intent. In all other respects, the "singleInstance" mode is identical to "singleTask".

Why singleTask is working?

Because singleTask doesn't require the creation of a new task for the new activities being launched.

enter image description here

I quoted from this answer to explain the difference between the two launch modes.

指尖微凉心微凉 2024-11-23 14:52:48

您是否在登录活动中声明了 setResult(int resultCode, Intent data)
Android 文档说
“请注意,此方法只能与定义为返回结果的 Intent 协议一起使用。”如果不是,或者某些其他条件符合 Activity.startActivityForResult() 的文档,则它将不会在您的任务中运行,因此您将立即收到取消结果。”

Have you declared the setResult(int resultCode, Intent data) in your Login activity.
Android Documentation says
"Note that this method should only be used with Intent protocols that are defined to return a result." If it is not or some other conditions matches as in the documentation of Activity.startActivityForResult(), it will not run in your task and thus you will immediately receive a cancel result."

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