Syntax_Error
Syntax_Error

Reputation: 6220

How to access Activity Variables from a fragment Android

In the Activity I have :

public class tabsmain extends Activity{
    public static Context appContext;

    public boolean lf_ch=false;

    public void onCreate(Bundle savedInstanceState){

I would like to access and possibly change lf_ch from a fragment inside tabsmain;

public class tabquests extends Fragment{ 
    public CheckBox lc;
@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)//onCreateView
    { 
lc.setChecked(//set it to lf_ch);

However, I can't seem to access the value of lf_ch.

Upvotes: 75

Views: 97645

Answers (14)

Mark Dail
Mark Dail

Reputation: 500

I know this is an old question, however here is an easier answer that work without jumping through any hoops. In your Fragment, define a variable that would reference the parent Activity. Then in the Fragment's onCreateView, connect the variable to the Activity and then you have a reference that you can use to get any public variable in the MainActivity. I had forgotten it when I ended up here. It's a little hard to do the other way as you need to find the exact fragment that is showing. However with this you shouldn't need to do it the other way, because you can easily pass things back and forth. I hope this help anyone that comes across it.

public Quiz_Avtivity mainQuiz;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_quiz, container, false);

mainQuiz = (Quiz_Avtivity) getActivity();
//Below is where you get a variable from the main activity
mainQuiz.AnyPublicVariable = whatEver;
//also
whatEver = mainQuiz.AnyPublicVariable

Upvotes: 33

Rohit Singh
Rohit Singh

Reputation: 18212

Make a generic Result receiver

enter image description here

You can create an interface for this task which would fetch a String data from any Activity to your Fragment. Follow these steps.

Create an interface

public interface MyResultReceiver{

      public String getResult();     

} 

Make MyResultReceiver a member of your Fragment

public class tabquests extends Fragment{ 

    public CheckBox lc;
    public MyResultReceiver resultreceiver;

    @Override
    public void onAttach(Context context){
         super.onAttach(cotext);
         resultreceiver = (MyResultReceiver)context;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)//onCreateView
    { 

           YourFragment code code

           Boolean result = resultreceiver.getResult();
           lc.setChecked(result);

     }

}

Implement MyResultReceiver in the Activity and override the method

public class tabsmain extends Activity implements MyResultReceiver{

        public boolean lf_ch=false;

        // Activity code


        @Override
        public boolean getResult(){
             return lf_ch;
        }

 }

Disclaimer:

You might find it a bit lengthy for this case. But the plus point of this approach is that if you want to reuse this code for another activity. You will not have to write the same logic again. Just implement the MyResultReceiver in your activity , override the method and your will be good to go.

TIP: To be able to get any kind of data, change the method definition in the interface
from public String getResult(); to public Object getResult();

Upvotes: 7

Solution : You can try this.

In tabquests Fragment use this,

public class tabquests extends Fragment{ 
    private tabsmain tabsmainActivity;
    public CheckBox lc;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup 
         container,Bundle savedInstanceState)//onCreateView
    { 
        tabsmainActivity = (tabsmain)getActivity; //typecasting 
        //now you can access the variables of tabsmain activity and make 
        //sure you give them public access in the activity`
        tabsmainActivity.lf_ch; //or do whatever operation you want here.
        lc.setChecked(//set it to lf_ch);
    }

Upvotes: 0

Shubham
Shubham

Reputation: 535

if you are using Java, you can use

((YourActivityName)getActivity()).variableName

to access, and if you are using Kotlin, you can use

(activity as YourActivityName).variableName

If the variable is defined as null in kotlin, you have to try each of these method as well:-

(activity as? YourActivityName).variableName

(activity as? YourActivityName)!!.variableName

or have to use let block, if possible.

Choose the correct one for you!

Hope, It will help.

Upvotes: 32

The Godfather
The Godfather

Reputation: 1

note that your fragment loads before the activity. so, you have to call the

tabsmain tabsm=(tabsmain) getActivity();

line in onActivityCreated() method

Upvotes: 0

Android Geek
Android Geek

Reputation: 9225

Access Activity variables in fragment to use static keyword like this:

MainActvity.java

public static boolean lf_ch=false;

tabquestsFragment.java

boolean if_value=MainActvity.lf_ch;

I hope it helps you

Upvotes: 0

Sanjay Goswami
Sanjay Goswami

Reputation: 191

take Activity value in fragment.

((MainActivity) getActivity()).mGoogleApiClient;

Upvotes: 5

Ashish Gupta
Ashish Gupta

Reputation: 747

Try Something like this:

    ViewPager mViewPager = (ViewPager) getActivity().findViewById(R.id.m_view_pager);

Upvotes: 0

Penzzz
Penzzz

Reputation: 2973

Another way to get data from activity is to access activity's intent via:

getActivity.getIntent().getExtras();

and etc.

It can be useful if you starts activity with fragment in xml, and would like to control somehow fragment's onCreate() behavior.

PS: of cause you should firstly put something to intent

Upvotes: 3

Vigneshwaran Murugesan
Vigneshwaran Murugesan

Reputation: 757

You could try the following method:

lc.setChecked(((yourpackagename)getActivity()).lf_ch);

Upvotes: 2

stalker
stalker

Reputation: 55

Change: public boolean lf_ch=false; to: public static boolean lf_ch=false; You can access/change the value with: tabsmain.lf_ch

Upvotes: -2

luchito
luchito

Reputation: 1

try this

public boolean lf_ch=false;
public class tabsmain extends Activity{

    public static Context appContext;
    public void onCreate(Bundle savedInstanceState){

Upvotes: 0

David M
David M

Reputation: 2541

Try this:

public View onCreateView(...){
  tabsmain xxx = (tabsmain)getActivity();
  lc.setChecked(xxx.lf_ch);
}

Upvotes: 68

Sumant
Sumant

Reputation: 2795

try tabsmain.appContext.lf_ch will give u value of that variable.

Also in that activity set appContext = this

Upvotes: 0

Related Questions