Complete FragmentActivity from a static method

I am trying to end my class that extends FragmentActivity from a static method: -

 private static void myFinish(Context context,Activity activity) { [some code] activity.finish(); } 

From this method: -

 private static void remove(Context context) { myFinish(context, ((Activity) context).getParent()); } 

This code is compiles , but gives me an error: -

 java.lang.ClassCastException: android.app.Application cannot be cast to android.app.Activity 

How can i solve this?

+2
java android casting exception
source share
4 answers

I found a simple solution. Declare:

 Activity activity; 

In onCreate:

 activity = this; 

then

 activity.finish(); 

does the work.

+5
source share

I assume you want to end your FragmentActivity from one of your snippets.

Here's how it works:

getActivity().finish();

If you are in FragmentActivity itself, you can use finish() .

+2
source share

It worked for me. Hope this helps.

 ((Activity)context).finish(); 
0
source share

Take an EmptyActivity action, call EmptyActivity by Intent, just end up EmptyActivity, but put android: noHistory = "true" in EmptyActivity in manifest

0
source share

All Articles