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.