Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finish Activity() from a separate myJavaClass.java

I have tried almost all the solutions from SO but no success :(.

I have a simple myJavaClass.java with a couple of functions.

One of the functions in myJavaClass : startActivity() starts MyCustomActivity

public startActivity(Context context)
{
    Intent intent = new Intent(context, MyCustomActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |Intent.FLAG_ACTIVITY_SINGLE_TOP);
    context.startActivity(intent);

}

This launches MyCustomActivity() as expected.

Now I have another function in myJavaClass.java to close/finish MyCustomActivity but it is not able to do so!

I have tried

  1. Making MyCustomActivity SingleTop in manifest and creating the activity via an intent as above

  2. Passing an activity instance to "this" in onCreate() of MyCustomActivity and calling MyCustomActivity.activity.finish() from myJava.class but that doesnt work as well

Please help me. I have been stuck here for hours now. I know the solution is very simple and conceptual but I am a newbie. Just building Java/Android concepts!

EDIT

MyCustomActivity

public Activity activity;

OnCreate()
{
    ...
    this = activity;
}

MyJavaClass

public closeActivity(Context context)
{

        Activity customActivity = MyCustomActivity.activity;
        customActivity.finish();
}
like image 487
Vikas Singh Avatar asked Dec 10 '22 01:12

Vikas Singh


1 Answers

I think that what you are trying to do is fundamentally bad. For a start, outside of the Activity code, there are no guarantees that the activity still exists - the memory manager may have cleaned it up, the user may have pressed Back etc. Think of Activities as independent entities - you can start them, and you can optionally get a result back when they finish what they're doing, but that's it.

Think about whether you really have to programmatically close the activity from outside it - I'd say this is an unusual design, but there are circumstances where it may be appropriate.

If so, what I think you want is a publish/subscribe system whereby MyCustomActivity can register a listener with MyJavaClass, and then receive a callback whereupon it can 'finish' itself.

public Activity activity implements FinishListener
{
  public void onCreate(...)
  {
      //where does MyJavaClass come from? see in a minute
      MyJavaClass myjava = getMyJavaclass();

      myJava.addFinishListener( this );
  }

  public void onFinishCallback()
  {
      this.finish();
  }
}

and

   public class MyJavaClass
   {
      private List<FinishListener> finishListeners = ...;

      public void addFinishListener( FinishListener fl )
      {
         this.finishListeners.add(fl);
      }

      public closeActivity(Context context)
      {
         for ( FinishListener fl : finishListeners )
         {
            fl.onFinishCallback();
         }
      }
   }

and

   public interface FinishListener
   {
       void onFinishCallback();
   }

Now the only remaining issue is how to get MyJavaClass from the Activity. That's up to you - you may already know how, you may be able to put it in your Application implementation, it could be a singleton (bad), the listeners could be static (bad) or various other options.

Oh, and don't forget to remove the listener again in the Activity's onDestroy() method!

like image 75
Rob Pridham Avatar answered Jan 05 '23 17:01

Rob Pridham