Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generic Class Arguments in Java

Tags:

java

android

So I'm passing Activities and Intents all over the place in my Android app to handle threading (I'm sure there is a better paradigm for this, as my code is starting to get annoyingly spaghetti-ish - usually a sign I'm not doing something right).

But anyways, I'm trying to do something like this:

public class SomeCoolActivity extends Activity {
    private class DoSomeTaskThatInvolvesEitherTheNetworkOrASleepStatementWithoutScrewingUpTheUiThread extends AsyncTask<Void, Void, Void> {
        @Override
        protected Void doInBackground(Void... params) {
            // Do something useful on a non-UI thread...
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            // Now that's done, I need to progress to the next activity. If I do that here,
            // I will get errors. So instead, I will call a method on my parent activity class:
            loadNextActivity(NextActivity.class);
        }
    }

    protected void loadNextActivity(Class<Activity> activityToLoad) {
        startActivity(new Intent(this, activityToLoad));
    }
}

But this does not work, as it tells me there is an error, that

NextActivity.class

is not Activity. Yes, obviously it's not the exact same class, but one would expect me to be able to pass in ANY class that derives from Activity (including NextActivity), but not any class which does not (it does in fact work fine if I define loadNextActivity as

protected void loadNextActivity(Class<?> activityToLoad)

but that's less than ideal as then I could pass in anything I want, perhaps something which is not an Activity...this is Java, not Ruby, after all). And besides, if it only allows me to pass in exactly one class...what on earth good is it?

So how do I make this work?

like image 647
mschultz Avatar asked Nov 29 '12 18:11

mschultz


People also ask

What is generic type arguments?

The generic argument list is a comma-separated list of type arguments. A type argument is the name of an actual concrete type that replaces a corresponding type parameter in the generic parameter clause of a generic type. The result is a specialized version of that generic type.

What are generic classes in Java?

A Generic class simply means that the items or functions in that class can be generalized with the parameter(example T) to specify that we can add any type as a parameter in place of T like Integer, Character, String, Double or any other user-defined type.

What is generics in Java with example?

The Java Generics allows us to create a single class, interface, and method that can be used with different types of data (objects). This helps us to reuse our code. Note: Generics does not work with primitive types ( int , float , char , etc).


1 Answers

You can write wildcard like:

protected void loadNextActivity(Class<? extends Activity> activityToLoad)

Its not good practice to use Class<?>. You won't know the exact type of the class token. Hmm, try to define global behavior for your method like I wrote above by using wildcard.

By the way be sure that each activity you add into the Manifest.xml file, like:

<activity android:name=".com.bla.bla.YourOtherActivity" />
like image 172
Maxim Shoustin Avatar answered Sep 29 '22 12:09

Maxim Shoustin