开发者

Call function from main activity in Android

I have main activity (Activity started at beginning in singletop mode)... Than in another Activity I want to access function (below) in main activity

public boolean checkConne开发者_如何学Goctions()
{

    if (isOnline() == false)
    {
        AlertDialog alertDialog = new AlertDialog.Builder(this).create();
        alertDialog.setTitle("Ni povezave!");
        alertDialog.setMessage("Pred uporabo je potrebno napravo povezati na internet!");
        alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
          public void onClick(DialogInterface dialog, int which) {

            return;

        } }); 
        alertDialog.setButton2("Poizkusi ponovno", new DialogInterface.OnClickListener() {
              public void onClick(DialogInterface dialog, int which) {

                return;

            } }); 
        alertDialog.show();
        return false;
    }
    else
    {
        return true;
    }
}

Is this possible? If it is I would like to ask how...


You have to either inherit this main Activity or make this method static. You can't properly invoke this method from another activity even if you'll have a reference to the main activity there because the view (dialog) needs a visible activity as a context.


No, the way you have described is not possible, as your main activity is invoked first, if your another activity from which you want to invoke that function is not initialized, then that function can not be called. I suggest to put this function so that it can be reused. something like in singleton pattern class/utility class

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜