Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: How to put Class type in a Bundle?

Tags:

android

bundle

Is it possible to put a Class type in a Bundle ?

public static <T> Intent newInstance(Class<T> EventClass) {
    Bundle args = new Bundle();
    args.putXXXX(EventClass);
    Intent intent = new Intent(MyApplication.getInstance(), MyActivity.class);
    intent.putExtras(args);
    return intent;
}

Thanks guys !

like image 877
anthony Avatar asked Aug 31 '15 14:08

anthony


People also ask

Can we pass object in bundle?

It is also possible to pass your custom object to other activities using the Bundle class. There are two ways: Serializable interface—for Kotlin and Android.

What is bundle class in Android?

Android Bundles are generally used for passing data from one activity to another. Basically here concept of key-value pair is used where the data that one wants to pass is the value of the map, which can be later retrieved by using the key.

Are bundles Parcelable?

Bundle is a container for named values of types standard for android (including Parcelable ), which is used to pass data between activies, fragments and other android app entites.

Is bundle a class package or an object?

A Bundle is like all Java classes is an Object. Objects do have constructors.


1 Answers

You can simply use Class<T>.getCanonicalName() and then instantiate the class by that name when reading it from the Bundle.

The scenario makes sense if for example you want to communicate between Android components which singleton classes to use.

As an example you could have an action interface

public interface Action {
    void run(List<String> args, Context context);
}

that should perform specific tasks in a Fragment and you want to initialize Fragments with different sets of actions. Then your Fragment's instantiation methods can simply take Action classes as parameter:

public static void newInstance(String someParameter, Class<? extends Action>... actions) {
    Bundle arguments = new Bundle();
    String[] actionNames = new String[actions.length];
    for (int i = 0; i < actionNames.length; i++) {
        actionNames[i] = actions[i].getCanonicalName();
    }
    arguments.putStringArray(ARG_ACTIONS, actionNames);
}

Having defined some classes MyActionA implements Action and MyActionB implements Action you would create a Fragment with these actions in the following way:

Fragment myFragment = MyFragment.newInstance("some parameter", MyActionA.class, MyActionB.class)

The Fragment itself can use a list of actions (List<Action> action) as member variable which is initialized from the Bundle in onCreate():

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    for (String actionName : getArguments().getStringArray(ARG_ACTIONS)) {
        try {
            Class<? extends Action> actionClass = (Class<? extends Action>) Class.forName(actionName);
            Action action = actionClass.newInstance();
            actions.put(action.getName(), action);
        } catch (java.lang.InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
}

This by the way would fail with anonymous classes when trying to instantiate them as getCanonicalName() returns null for anonymous classes. So this method cannot be used to pass around anonymous classes, which I doubt to be possible at all.

like image 118
user905686 Avatar answered Oct 17 '22 15:10

user905686