2

After seeing this question, it got me thinking. I can get a Intent in a Fragment by calling this inside onCreateView:

String Item = getActivity().getIntent().getExtras().getString("name");

the problem with this is that getActivity might return null, to counter that I can call:

if(getActivity() != null)
    String Item = getActivity().getIntent().getExtras().getString("name");
}

this will work fine, but..


I was thinking of creating a static method in my Activity and then accessing the Intent in my fragment by calling that method, like this (In my Activity):

public class DemoActivity extends Activity{
    static String name;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_demo);
        //Getting the Intent from the previous Activity
        name = getIntent().getStringExtra("name");
    }

    public static String Name(){
        //returning the Intent
        return name;
    }

}

Then in my Fragment I can call this like this:

String name = DemoActivity.Name();

My Question:

Can I do it like this? Will it cause any issues and why?


Currently

It is working fine.

HB.
  • 4,116
  • 4
  • 29
  • 53
  • its working fine, but static method can always case some memory-leaks. Intent can be preferable. – Sachin Rajput Jul 25 '18 at 08:00
  • @Thunder I'm not storing the `String`, I'm passing it to the `Fragment` - https://stackoverflow.com/a/43480386/5550161 – HB. Jul 25 '18 at 08:12
  • `getActivity()` will only return `null` if you created the `Fragment` from outside an `Activity`. If you are always creating the `Fragment` within an `Activity`, then `getActivity()` should never return `null`. – David Wasser Jul 25 '18 at 08:20
  • variable name should be static so that it is accessible by a static method, here the function Name(), cannot return name unless name is a static variable – Ramees Thattarath Jul 25 '18 at 08:22
  • @DavidWasser Currently a `Fragment` calls the `Activity` that holds the `Fragment`. In other words, `Activity1` holds a fragment, that fragment starts the second activity `Activity2` and `Activity2` holds the fragment I want to get the intent. – HB. Jul 25 '18 at 08:30
  • @RameesThattarath Can you please elaborate? Are you talking about `String name;` should be `static String name;`? It works fine currently by just setting it as `String name;` – HB. Jul 25 '18 at 08:32
  • `Fragment`s don't **call** `Activity`s. They launch them. In most cases, a `Fragment` is tied to an `Activity`, which means that getActivity()` will never return `null`. It only returns `null` in special cases where the `Fragment` is created but not associated with an `Activity`. – David Wasser Jul 25 '18 at 08:38
  • What I am saying is that you can do `String Item = getActivity().getIntent().getExtras().getString("name");` without worrying about `getActivity()` returning `null`. – David Wasser Jul 25 '18 at 08:39
  • @DavidWasser Creates is what I meant. So I don't have to worry about the `getActivity` returning `null`. Call you please clarify why what I'm doing is not correct? – HB. Jul 25 '18 at 08:40
  • Static methods cannot refernce instance variables as instance variables are meant for each objects and static method is called over the class name and not with respect to any objects- This code should return following error non-static variable name cannot be referenced from a static context – Ramees Thattarath Jul 25 '18 at 08:49
  • You can also do what you are doing. It is just unnecessarily complicated. There's nothing wrong with it. There are many ways to do things in software. Your approach is just unnecessarily convoluted. It also doesn't solve the problem. If your `Fragment` can exist without the `Activity`, then theoretically `DemoActivity.Name()` can **also** return `null`! – David Wasser Jul 25 '18 at 08:49
  • @DavidWasser Thank you, that was the answer I was looking for. I know that a `Fragment` cannot exist on its own that is why `getActivity()` must be called. It just makes more sense to me doing it this way. That is why I asked the question to find out if there is any difference. – HB. Jul 25 '18 at 08:54
  • @RameesThattarath `This code should return following error non-static variable name cannot be referenced from a static context` Sorry you are right, in the question it is not static, but in my code it is. I will edit the question. – HB. Jul 25 '18 at 09:19

2 Answers2

4

try like this:

Activity class:

 Bundle bundle = new Bundle();
 bundle.putString("your_key", "your_value");
 your_fragment.setArguments(bundle); 

Fragment class:

String your_variable = getArguments().getString("your_key");
Mani Vasagam
  • 820
  • 5
  • 10
  • `Bundle` is created in a Fragment of the previous Activity. – HB. Jul 25 '18 at 08:35
  • `Bundle` is attached to the fragment to be created, and you can verify his existance wtih `if(getArguments() != null && getArguments.containsKey("your_key"))` validation. This is the best practice and most common one to use. Regarding your question, what would happen if your activity gets destroyed? Maybe we are not understanding what's your goal here. You just want to pass data from an Activity to a fragment? Is that it? – miguelarc Jul 25 '18 at 08:38
  • I want to pass data from a Fragment in one Activity to a Fragment in another Activity. – HB. Jul 25 '18 at 08:47
  • @HB. I have posted another answer for above qusetion – Mani Vasagam Jul 25 '18 at 09:18
1

Set in first activity fragment:

Bundle bundle = new Bundle();
bundle.putString("your_string_key", "your_value");

startActivity(new Intent(getActivity() your_second_activity.class).putExtra("bundle_key", bundle));

Get bundle value second activity:

fragment.setArguments(getIntent().getBundleExtra("bundle_key"));

In Second Activity Fragment:

getArguments().getString("your_string_key")
Mani Vasagam
  • 820
  • 5
  • 10
  • you want to pass data from first activity fragment to second activity fragment right??? – Mani Vasagam Jul 25 '18 at 09:23
  • `My Question` -> Can I do it like this? Will it cause any issues and why? – HB. Jul 25 '18 at 09:24
  • Yeah you can do.. no issue – Mani Vasagam Jul 25 '18 at 09:26
  • something is wrong in Kotlin. not working -- everything is null from the Activity to the Fragment. there are constructs of "arguments" and requiresArgs() and nothing is working as expected. back to the books! Once again the compiler geniuses of Kotlin compiler has taken a simple, working concept and made it impossible to get right. – mobibob Sep 10 '20 at 23:08