我应该如何通过活动链转发Intent参数?

时间:2022-06-24 06:54:34

I have lots of Activities chained together with Intents and some Intents require parameters passed in the extras Bundle. When I have to forward parameters through multiple Activities, should I copy each one explicitly or is there a best-practice way of doing it? For instance, I could clone-copy the current Intent as a starting point for calling other subtask Intents, and this would (presumably) copy all previous Bundle parameters.

我有许多活动与Intents链接在一起,而一些Intents需要在附加组件Bundle中传递参数。当我必须通过多个活动转发参数时,我应该明确地复制每个参数还是有最佳实践方法?例如,我可以克隆复制当前的Intent作为调用其他子任务Intents的起点,这可能(可能)复制所有以前的Bundle参数。

As an illustration, say you have a file explorer Activity that is in one of two modes: Expert and Novice. You want to pass this state to some subtask Activity like a file properties page, which you could do by calling putExtra("skillLevel", "Expert") on the Intent before you launch it. Now if the property page also has a subtask Activity, compression options for instance, how should you forward on the "skillLevel" parameter?

举个例子,假设你有一个文件浏览器Activity,它有两种模式:Expert和Novice。您希望将此状态传递给某个子任务Activity,如文件属性页,您可以在启动它之前通过调用Intent上的putExtra(“skillLevel”,“Expert”)来执行此操作。现在,如果属性页面还有一个子任务Activity,例如压缩选项,你应该如何转发“skillLevel”参数?

4 个解决方案

#1


20  

I think the best and cleaner way to do it is to initialize the next Intent using the received Intent through the Intent(Intent) constructor.

我认为最好和更简洁的方法是使用通过Intent(Intent)构造函数接收的Intent初始化下一个Intent。

Intent newIntent = new Intent(receivedIntent);

#2


57  

I dont know why you would want to copy all the other properties using a constructor.

我不知道你为什么要使用构造函数复制所有其他属性。

intent.putExtras(oldIntent);

Should do the trick. Note that intent is an newly created instance!

应该做的伎俩。请注意,intent是一个新创建的实例!

#3


2  

If a parameter is system wide, until it is changed (such as difficulty of a game), it may be easier to story it in Shared Preferences. It would have the side effect of remembering the difficulty set when the user leaves the app.

如果参数是系统范围的,直到它被更改(例如游戏的难度),在共享首选项中可能更容易编辑它。当用户离开应用程序时,它会产生记住难度设置的副作用。

#4


0  

Since we don't have Global variables in Android you can create a class with your application wide informations and use the Singleton pattern . Since it will be changed for all the system, this way you can always get the same instance of this object, hence always the same information.

由于我们在Android中没有全局变量,因此您可以使用应用程序范围的信息创建一个类并使用Singleton模式。由于它将针对所有系统进行更改,因此您始终可以获得此对象的相同实例,因此始终使用相同的信息。

An example:

public class Object {

private static Object instance;

private Object objectcall;  

private Object(){

}

public void setObject(Object newObject){
    this.objectcall = newObject;

} 


public  Object getObject(){
    return this.objectcall;
}


public static synchronized Object getInstance(){

         if(instance==null){    
           instance=new Object();
         }
         return instance;

}
}

when you want to retrieve it in a Activity just call

当你想在一个Activity中检索它时

Object objectSingleton = Object.getInstance();

#1


20  

I think the best and cleaner way to do it is to initialize the next Intent using the received Intent through the Intent(Intent) constructor.

我认为最好和更简洁的方法是使用通过Intent(Intent)构造函数接收的Intent初始化下一个Intent。

Intent newIntent = new Intent(receivedIntent);

#2


57  

I dont know why you would want to copy all the other properties using a constructor.

我不知道你为什么要使用构造函数复制所有其他属性。

intent.putExtras(oldIntent);

Should do the trick. Note that intent is an newly created instance!

应该做的伎俩。请注意,intent是一个新创建的实例!

#3


2  

If a parameter is system wide, until it is changed (such as difficulty of a game), it may be easier to story it in Shared Preferences. It would have the side effect of remembering the difficulty set when the user leaves the app.

如果参数是系统范围的,直到它被更改(例如游戏的难度),在共享首选项中可能更容易编辑它。当用户离开应用程序时,它会产生记住难度设置的副作用。

#4


0  

Since we don't have Global variables in Android you can create a class with your application wide informations and use the Singleton pattern . Since it will be changed for all the system, this way you can always get the same instance of this object, hence always the same information.

由于我们在Android中没有全局变量,因此您可以使用应用程序范围的信息创建一个类并使用Singleton模式。由于它将针对所有系统进行更改,因此您始终可以获得此对象的相同实例,因此始终使用相同的信息。

An example:

public class Object {

private static Object instance;

private Object objectcall;  

private Object(){

}

public void setObject(Object newObject){
    this.objectcall = newObject;

} 


public  Object getObject(){
    return this.objectcall;
}


public static synchronized Object getInstance(){

         if(instance==null){    
           instance=new Object();
         }
         return instance;

}
}

when you want to retrieve it in a Activity just call

当你想在一个Activity中检索它时

Object objectSingleton = Object.getInstance();