Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Fragment cannot get activity

I have an activity which does a fragment transaction

DetailFragment newFragment = new DetailFragment();
transaction.replace(R.id.mylist, newFragment);
transaction.addToBackStack(null);
transaction.commit();

that works fine. Now I know within my activity a dynamic string I need to replace in the layout I have in newFragment. I thought that I can call after transaction.commit() just something like

newFragment.setMyString("my dynamic value");

And in the newFragment.java I have

public void setMyString(String s)
{
 TextView tv = (TextView) getActivity().findViewById(R.id.myobject);
 tv.setText(s);
}

The point is that getActivity() returns null. How can I get the context I need to find my layout elements?

Edit:

I try to follow the route using a bundle as this seems to be the cleanest way. So I have changed my code:

Bundle b = new Bundle();
b.putString("text", "my dynamic Text");
DetailFragment newFragment = new DetailFragment();
transaction.replace(R.id.mylist, newFragment);
transaction.addToBackStack(null);
transaction.commit();

My fragment onCreateView looks like this:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
{
   View v = inflater.inflate(R.layout.mylayout, container, false);
   TextView t = (TextView) v.findViewById(R.id.texttobereplaced);
   t.setText(savedInstanceState.getString("text");
}

It seems that savedInstranceState is empty. Where should I find my bundle?

Edit2:

missed the getArguments() in the reply. is working now.

like image 295
AndyAndroid Avatar asked Jul 31 '12 15:07

AndyAndroid


People also ask

How to use fragment activity in Android?

Add a fragment to an activity You can add your fragment to the activity's view hierarchy either by defining the fragment in your activity's layout file or by defining a fragment container in your activity's layout file and then programmatically adding the fragment from within your activity.

Can we use Onactivityresult in fragment?

We can call startActivityForResult directly from Fragment but actually mechanic behind are all handled by Activity. Once you call startActivityForResult from a Fragment, requestCode will be changed to attach Fragment's identity to the code.

Does fragment have its own context?

The fragment has different notifications, one of them being onActivityCreated. You can get the instance of the activity in this lifecycle event of the fragment. Then: you can dereference the fragment to get activity, context or applicationcontext as you desire: this.

Is attempting to registerForActivityResult after being created fragments must call registerForActivityResult () before they are created?

registerForActivityResult() is safe to call before your fragment or activity is created, allowing it to be used directly when declaring member variables for the returned ActivityResultLauncher instances.


3 Answers

Make sure you call getActivity() in or after onAttach(), as before then it will return null.

like image 87
Alex Lockwood Avatar answered Sep 25 '22 22:09

Alex Lockwood


Your Fragment hasn't attached to the Activity yet, which also means your layout hasn't been inflated yet (See the Fragment lifecycle). The best solution here would be to add your String value as an argument to the Fragment via the Fragment.setArguments(Bundle) method. This can be retrieved in the receiving Fragment via the Fragment.getArguments() method.

like image 44
Jason Robinson Avatar answered Sep 25 '22 22:09

Jason Robinson


So i am also using the same fragment transaction manager and the only problem i can see with your code is that you define the TextView tv inside your onCreateView method of your newFragment class and then instantiate it as like this :

public class AboutFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.newFragment, container, false);
        TextView tv = (TextView) v.findViewById(R.id.textview);
        return v;
    }

    public void setMyString(String s) {
        tv.setText(s);
    }
}

I know it does'nt make really much sense but that is how i am running my code and it works fine :)

like image 37
Android2390 Avatar answered Sep 25 '22 22:09

Android2390