简体   繁体   English

Android:是否有可能在未来进行一些活动?

[英]Android: Is it possible to pass intent several activities ahead?

For example: I have 5 activities: 例如:我有5个活动:

Activity A --> Activity B --> Activity C --> Activity D --> Activity E 活动A->活动B->活动C->活动D->活动E

Activity A must go through Activity B then C then D then E in sequence . 活动A 必须顺序进行活动B,然后是C,然后是D,然后是E。

I want to pass an intent in Activity A to Activity E without passing it through B, C, D. 我想将活动A的意图传递给活动E, 而不通过B,C,D。

Is it possible to do that? 有可能这样做吗?

My passing intent in Activity A currently: 我目前在活动A中传递的意图:

fab = (FloatingActionButton) rootView.findViewById(R.id.fabAddConList);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent createContactList = new Intent(context, ActivityB.class);
                createContactList.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
                createContactList.putExtra("userid", userID);
                startActivity(createContactList);
            }
        });

My receiving intent in Activity E currently: 我目前在活动E中的接收意图:

 Intent lastIntent = getIntent();
        userid = lastIntent.getStringExtra("userid");

-----------------------------------EDIT----------------------------------------- - - - - - - - - - - - - - - - - - -编辑 - - - - - - - ---------------------------

As based on my codes in this post, I am passing the intent in Activity A to Activity B instead of E, because I want to go to Activity B when I click the button. 根据本文中的代码,我将活动A的意图传递给活动B而不是活动E,因为单击按钮时我想转到活动B。 But how can I go to Activity B without passing the "userid" to Activity B and keep it till it reached Activity E? 但是,如何在不将“用户ID”传递给活动B并将其保留到活动E的情况下进入活动B?

No, it is not possible to have an Activity pass an Intent to the next Activity in the sequence without first receiving that Intent . 不,这是不可能有一个Activity传递一个Intent到下一个Activity的序列中而无需首先接收到Intent How else to pass it on? 还有什么要继续下去的?

In your case, consider writing the piece of information for Activity E to SharedPreferences . 在您的情况下,请考虑将Activity E的信息写入SharedPreferences They use key-value pairs for storing data just like intents do, see also this link to documentation . 他们像意图一样使用键值对来存储数据,另请参见此文档链接

By the way, from a security point of view: at least in some versions of android, the Intent used with startActivity() is not private to your app. 顺便说一句,从安全角度来看:至少在某些android版本中,与startActivity()一起使用的Intent不是您的应用专用的。 So be careful not to pass sensitive data with startActivity() . 因此请注意不要使用startActivity()传递敏感数据。

Yes, As long as intent is not clear everything will pass with intent from start to end. 是的,只要意图不明确,一切都会从头到尾通过。 Whenever you want to clean intent data you can call 每当您要清除意图数据时,您都可以致电

getIntent().removeExtra("key"); 

If you want to forward the intent. 如果您要转发意图。 You can do it by 你可以做到

Intent newIntent = new Intent(receivedIntent);

No, you have to pass value from all activities. 不,您必须传递所有活动的价值。

Otherthan this you can use SharedPreferences to store value in Activity A and fetch that value in Activity E . 除此之外,您可以使用SharedPreferences将值存储在Activity A并在Activity E获取该值。 check this for reference. 检查以供参考。

Other way is static variable, take public static variable in A and access it from Activity E 另一种方法是静态变量,在A中获取public static变量,然后从Activity E访问它

in Activity A 在活动A中

public static String yourVariable="value";

in Activity E access it by using A.yourVariable 在活动E中,使用A.yourVariable访问它

For my app, the user will have to go through activity A to E in sequence. 对于我的应用程序,用户将必须按顺序执行活动A到E。 The user cannot go from Activity A to Activity E straight without passing B,C and D. Can I still pass the intent from A to E when process is still going in sequence like A,B,C,D, E? 用户不能在没有通过B,C和D的情况下直接从活动A转到活动E。当流程仍按A,B,C,D,E顺序进行时,我是否仍可以将意图从A传递到E?

your all activity in sequence then you must have to pass value from all activities using intent. 您的所有活动都按顺序进行,那么您必须使用意图传递所有活动的价值。

alternative 替代

use Preference to store your value or static variable 使用Preference存储您的值或static variable

One option is to use a public static variable (or singleton) to store a value to be read at a later point by another activity. 一种选择是使用公共静态变量(或单例)来存储一个值,以供以后另一个活动读取。 Usually you don't want to use statics like this, but it might be the easiest thing for you to do. 通常,您不想使用这样的静态函数,但这对您来说可能是最简单的事情。

This is basic application of static variables in Java. 这是Java中静态变量的基本应用。 Declare a new class with the state you want to retain: 用您要保留的状态声明一个新类:

public class State {
    public static String value;
}

In Activity A: 在活动A中:

State.value = "value that you want to read in activity E"

In Activity E: 在活动E中:

String v = State.value;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM