0

I'm very new to android and I need your help.

I'm curious if what I do is the right thing or not.

My application has just only one Activity(MainActivity It's AppCompatActivity btw).

But I use several Fragment instead.

Some of Fragments contain an Object(I call it ProblemClass) that must have Activity(not Context though).

Such as ListView's Adapter(or something like that) to inflate Layout

or Dialog that have to inflate Layout and need Context to create AlertDialog.Builder .

And it(ProblemClass) can't call getActivity().

In my mind I have two solution.

1.Pass Activity to ProblemClass.

2.Holding Activity in Singleton for use across whatever Class entire application.

I chose the latter. But...

I have read somewhere that it may cause memory leaked, right?

Since Singleton-Object exist entire application lifetime.

.

.

This is my Singleton that hold Activity.

I call setMyActivity(MainActivity.this) in onCreate() in MainActivity to pass it to Singleton.

public class MyActivitySingleton 
{
    private AppCompatActivity mAppCompatActivity;

    private MyActivitySingleton()
    {

    }

    private static class MyActivitySingletonHolder
    {
        private static final MyActivitySingleton INSTANCE = new MyActivitySingleton();
    }

    public static MyActivitySingleton getInstance()
    {
        return MyActivitySingletonHolder.INSTANCE;
    }

    public AppCompatActivity getMyActivity()
    {
        return this.mAppCompatActivity;
    }

    public void setMyActivity(AppCompatActivity appCompatActivity)
    {
        this.mAppCompatActivity = appCompatActivity;
    }
}

Any suggestion? What do I have to do?

Pass an Activity to ProblemClass and if it need Context. It can use Activity as a Context too?

Thanks.

Math Burn
  • 89
  • 1
  • 6
  • in `Fragment` you can access `Activity` by using `getActivity()` – N J Aug 06 '15 at 10:00
  • yes but in other class like ListView.Adapter it can't. what do I have to do? – Math Burn Aug 06 '15 at 10:04
  • for adapter you can pass it as parameter – N J Aug 06 '15 at 10:08
  • so just pass 'Activity' from 'Fragment' to that ProblemClass right? and if I hold 'Activity' in 'Singleton' is it bad? since I have to pass 'Activity' to every 'Fragment' that currently working. at least it has to be one 'Fragment' that working. doesn't it will be the same as holding 'Activity' in Singleton. – Math Burn Aug 06 '15 at 10:12
  • *Such as ListView's Adapter(or something like that) to inflate Layout* ... just pass inflater or context via constructor ... as long as `ProblemClass` does live no longer than the Activity itself you can still pass Activity to the constructor and keep references ... if `ProblemClass` is living longer than activity you shouldn't do this ... – Selvin Aug 06 '15 at 10:14
  • you can store on singleton class but this is better approach – N J Aug 06 '15 at 10:15
  • and what about `Dialog`.it need `Inflater` and `Context`.just do the same thing right? since it need empty Constructor. can I just create a method that pass `Inflater` and `Context`? – Math Burn Aug 06 '15 at 10:19
  • anyway i don't see the point of such singleton (there will be nothing wrong with it but you have to set activity instance in `onStart` and set it back to null in `onStop`) ... so it would be usless as you can always pass instance of activity somewhere – Selvin Aug 06 '15 at 10:22
  • any suggestion? how to create it? I'm very new to this thing. – Math Burn Aug 06 '15 at 10:24
  • about dialog ... there is no sens of showing the dialog without the context ... so if you need to show dialog you always have a context ready to use ... – Selvin Aug 06 '15 at 10:24
  • *any suggestion?* yes, come back here with a real problem ... this singleton is bad ... but if you fix it: `setMyActivity(MainActivity.this)` in `onStart` and `setMyActivity(null)` in `onStop` I'm pretty sure that it would be usless... – Selvin Aug 06 '15 at 10:26
  • okay in Dialog I can call `getActivity()`. at first I just only trying to call get'Whatever'Context(). – Math Burn Aug 06 '15 at 10:28
  • get'Whatever'Context() makes no sens ... just make methods like `ProblemClass.giveMeSomthingThatNeedContext(Context ctx)` or `ProblemClass.giveMeSomthingThatNeedActivity(Activity act)` – Selvin Aug 06 '15 at 10:30
  • okay now I didn't need that Singleton anymore.thanks you guys. – Math Burn Aug 06 '15 at 10:52

1 Answers1

0

For Adapter on Fragment you can:

ArrayAdapter<String> adapter = ArrayAdapter<String>(getActivity(), stringList);
listView.setAdapter(adapter);
quan-ng
  • 84
  • 4