How to reference the current or main activity from another class

后端 未结 13 1065
难免孤独
难免孤独 2021-01-30 12:39

I often find myself needing to access methods that require referencing some activity. For example, to use getWindowManager, I need to access some Activity. But ofte

13条回答
  •  灰色年华
    2021-01-30 13:13

    Handle the Intent in the class you want to do these methods, and send your information to it in a Bundle like so:

        Intent i = new Intent("android.intent.action.MAIN");
        i.setComponent(new ComponentName("com.my.pkg","com.my.pkg.myActivity"));
        Bundle data = new Bundle();
    
        i.putExtras(data);
    
        startActivityForResult(i);
    

    Then use an OnActivityResultListener to grab the new data.

提交回复
热议问题