Reputation: 371
I did a search but I wasn't able to find nothing about it. The question is: I got a utility class were I want to insert a log out function that can be started from any activity in the project, and this is obviously easy, but I'm unable to define from what class this function is called, because I want to redirect the user to the login class after his log out, so I was thinking to pass the class as parameter, but I'm retrieving an error in the intent...how can I do that?
public final class utility {
private utility(){}
public static void logOut(String className) {
LoginManager.getInstance().logOut();
Intent i = new Intent(className.class, LoginActivity.class); //here I got the error unknown class className
startActivity(i);
}
}
Upvotes: 0
Views: 173
Reputation: 1868
The key is to use Activity Context when you start a new Activity or a Service. Remember that there are a lot of context types (App context, activity/service and more..).
If you are creating a utility class you have to be sure that you are sending the right context to it. The best way to do it is to send your current activity so the utility can use it's Context.
So the answer should be:
public static void logOut(Activity activity, String className) {
Intent i = new Intent(activity, LoginActivity.class);
activity.startActivity(i);
}
There is a great article about Context's I suggest you read that :)
Upvotes: 2
Reputation: 1043
Change your code to be like this:
public final class utility {
private utility(){}
public static void logOut(Context mContext) {
LoginManager.getInstance().logOut();
Intent i = new Intent(mContext, LoginActivity.class);
mContext.startActivity(i);
}
}
Upvotes: 2