English 中文(简体)
如何从其他类别向活动派出活动?
原标题:How to send the events to activity from other class?

我正试图将事件从一个贾瓦人阶层传到活动。

假设情况是,土著人口将掌握一些土著数据,因此,土著人口将称呼back功能。 这一类处理数据,经过处理,即需要更新数据分析。 我想在活动手里的某个地方更新情报调查。 (Dont 我想在任何地方使用OniThread()。)

I was not able to send the events properly with the below approaches.
1st Approach:
1) Define functions for posting messages in to the queue and call these functions.
2) To call the above mentioned functions (point 1) we need context, if i maintain the static variable for maintaining the context and returning it, if the activity is created twice we wont able to get the write context for the first activity.

public class Activity1 {
    protected static Context myContext = null;
    protected Handler myHandler = null;

    @override 
    public void onCreate() {
        myContext = this;
        myHandler = new Handler();
    }

    public static Context getMyContext() {
        return myContext;
    }

    public void postEvent1() {
        myHandler.sendMessage();
    }
}

2nd Approach:
1) Making the handler as a static variable and returning this with the help of static function. - Not a good design to expose the internal variables.
2) Cons will be like above, when a second activity is created.

public class Activity1 {
    protected static Handler myHandler = null;

    @override 
    public void onCreate() {
        myHandler = new Handler();
    }

    public static Context getMyHandler() {
        return myHandler;
    }
}

能否利用静态变量和静态功能来了解活动的背景?

如果任何人知道:

Thanks & Regards,
SSuman185

最佳回答

我使用一个集装箱舱HashMap与钥匙储存环境。

我将这一类别的名称作为关键。

当第二次活动试图在含有散射图的类别上登记时,它将对已经储存的活动(如果不是的话)作出答复。

因此,正如我一样,如果我创造第二个活动环境,我就能够储存这几类活动的背景,避免干扰第一项活动。

并请补充一下,是否有更好的解决办法。

问题回答

暂无回答




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签