简体   繁体   中英

Android - Hold Activity reference in application-level non-Activity class

I have an application class and it holds a reference of MyAdapter class:

public class MyApplication extends Application {

    ......

    private static MyAdapter sMyAdapter;

    public static MyAdapter getMyAdapter() {

        if (sMyAdapter == null) {
            sMyAdapter = new MyAdapter(this);
            MyApplication.setMyAdapter(sMyAdapter);
        }

        return sMyAdapter;
    }

    public static void setMyAdapter(MyAdapter myAdapter) {
        sMyAdapter = myAdapter;
    }

    ......
}

MyAdapter class is a customized android adapter class, and the application Context is passed to the Adapter. The application holds a reference of it because it may be used anytime till the application is still running.

The problem is that now I need an Activity Context in the Adapter to start another Activity when some button is clicked because if I use application Context I need to add a Intent flag FLAG_ACTIVITY_NEW_TASK which I don't want to because that way the new Activity being started will be running in a new task. I tried a lot with changing launch mode and taskAffinity but either new issues came up or the Activity will be running in a new task.

So I am thinking to hold an Activity reference which shows the button in the Adapter class, and to avoid memory leak, I came up with the following:

public class MyActivity extends Activity {

    ......

    @override
    public void onResume() {
        ......
        MyApplication.getMyAdapter().setActivity(this);
        ......
    }

    ......

    @override
    public void onDestroy() {
        ......
        MyApplication.getMyAdapter().setActivity(null);
        ......
    }
}

Then in the Adapter class I will use the Activity reference to start another Activity. I tested and this worked fine but the question is would this avoid memory leak and is this a proper way to hold the Activity reference when onResume and release it when onDestroy? Is there any other decent way to achieve my purpose? Thanks.

would this avoid memory leak

Not really. You MyApplication object will still keep a reference to your adapter with all it's 'contents' (further references).

Yes, you've got rid of keeping the destroyed Activity around, and you might feel it's okay to keep the adapter because you're 'going to need it again anyways', still this whole construct is a horrible code smell and will with certainty introduce new memory leaks and other problems as you develop this further. Logically, this adapter is part of an Activity , and as the Activity 'dies', so should the adapter.

I'm sure there's a reason why you felt you needed that adapter on your application, so I'd post another question asking 'how can I achieve soandso without my application knowing of my adapter'.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM