光洁度活性()从一个单独的myJavaClass.java [英] Finish Activity() from a separate myJavaClass.java

查看:160
本文介绍了光洁度活性()从一个单独的myJavaClass.java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试了几乎全部来自这样的解决方案,但没有成功。(

我有一对夫妇的功能,简单的myJavaClass.java。

一个在myJavaClass功能:startActivity()启动MyCustomActivity

 公共startActivity(上下文的背景下)
{
    意向意图=新意图(背景下,MyCustomActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    context.startActivity(意向);

}

这将启动预期MyCustomActivity()。

现在我在myJavaClass.java另一个功能来关闭/完成MyCustomActivity但它不能这样做!

我曾尝试


  1. 制作MyCustomActivity SingleTop清单中,并通过一个意图如上

    创建活动

  2. 传递活动实例,以本在MyCustomActivity中的onCreate(),并呼吁从myJava.class MyCustomActivity.activity.finish(),但是,这并不工作,以及


请帮助我。我一直在困在这里小时。我知道解决的办法很简单,概念阶段,但我是一个新手。只是构建Java / Android的概念!

修改

MyCustomActivity

 公众活动活动;在OnCreate()
{
    ...
    该=活动;
}

MyJavaClass

 公共closeActivity(上下文的背景下)
{        活动customActivity = MyCustomActivity.activity;
        customActivity.finish();
}


解决方案

我认为你正在试图做的是从根本上坏。一开始,该活动code外,没有保证,该活动仍然存在 - 内存管理器可能已清理它,用户可以有pressed后退等想想活动作为独立的实体 - 你可以启动它们,你可以选择得到的结果,当他们完成他们在做什么,但仅此而已。

想想你是否真正必须以编程方式从外面关闭活动 - 我会说这是一个不寻常的设计,但有些情况下,它可能是适当的。

如果是这样,你想要什么,我认为这是一个发布/订阅系统,使MyCustomActivity可以注册一个MyJavaClass监听器,然后接收回调,于是它能够完成本身。

 市民活动活动实现FinishListener
{
  公共无效的onCreate(...)
  {
      //哪里MyJavaClass从何而来?看到在一分钟内
      MyJavaClass米亚瓦= getMyJavaclass();      myJava.addFinishListener(本);
  }  公共无效onFinishCallback()
  {
      this.finish();
  }
}

 公共类MyJavaClass
   {
      私人列表< FinishListener> finishListeners = ...;      公共无效addFinishListener(FinishListener FL)
      {
         this.finishListeners.add(佛罗里达州);
      }      公共closeActivity(上下文的背景下)
      {
         对于(FinishListener佛罗里达finishListeners)
         {
            fl.onFinishCallback();
         }
      }
   }

 公共接口FinishListener
   {
       无效onFinishCallback();
   }

现在剩下的唯一问题是如何从活动得到MyJavaClass。这是给你 - 你可能已经知道怎么了,你可以把它放在你的应用程序实现,也可以是单身(坏),听众可以是静态的(坏)或各种其他选项

哦,不要忘记在活动的的onDestroy()方法再次删除监听器!

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();
}

解决方案

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!

这篇关于光洁度活性()从一个单独的myJavaClass.java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆