3

I am new to android and learning things with fragments and have made a demo for it,in that i am having a fragment from which we can go to another activity at there some calculation is performing and after that we come back to frgament at that time i want to dislay that calculation value to my fragment's textview,So which life cycle method should i use to do so?i already used onresume which is not working...

public void onResume () {

        super.onResume();

        //tvFollowings.setText((sharedConnect.getCurrentUser().userFollowingCount)
//              + " Following");
                System.out.print("------user count is-------" + String.valueOf(sharedConnect.getCurrentUser().userFollowingCount));
        Toast.makeText(getActivity(), "------user count is-------" + String.valueOf(sharedConnect.getCurrentUser().userFollowingCount), Toast.LENGTH_SHORT).show();

}
sulphuric Acid
  • 585
  • 6
  • 23

6 Answers6

2

Yout have to use startActivityForResult(...) when calling your activity, then you can get any information you need in your fragments onActivityResult().

David
  • 306
  • 6
  • 20
2

The best approach which works, toggle between onPause and onResume. No need to even bother the parent activity

private boolean allowRefresh = false;

@Override
    public void onResume() {
        super.onResume();
        //Initialize();
        if(allowRefresh){
            allowRefresh=false;
            //call your initialization code here
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        if (!allowRefresh)
            allowRefresh = true;
    }

onResume will always be called when your fragment gets loaded, so initial state of allowRefresh should be false so the fragment does not get loaded twice

Once you open new activity whilst the fragment is active, onPause is called, here set allowRefresh to true only if allowRefresh is false.

When the fragment regains focus, check if allowRefresh is true and redo your initialization. A good code practice is put all your initialization code in one function.

  • I am aware it has been almost 4 years since your answer, but after days of searching I've stumbled onto this and you saved me. Thank you so much! – Loizos Aristeidis Mar 22 '22 at 21:22
0

You can use Bundle to do the same in Android

Create the intent:

Intent i = new Intent(this, ActivityTwo.class);
AutoCompleteTextView textView = (AutoCompleteTextView)    findViewById(R.id.autocomplete);
String getrec=textView.getText().toString();

//Create the bundle

Bundle bundle = new Bundle();

//Add your data to bundle bundle.putString(“stuff”, getrec);

//Add the bundle to the intent

 i.putExtras(bundle);

//Fire that second activity

startActivity(i);

Now in your second activity retrieve your data from the bundle:

//Get the bundle

Bundle bundle = getIntent().getExtras();

//Extract the data…

String stuff = bundle.getString(“stuff”); 

You can refer here for more.POST

Community
  • 1
  • 1
Kristo
  • 1,339
  • 12
  • 22
0

You can use the onResume() method of the Fragment

public class Fragment_ABC extends Fragment {
    View view;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.fragment_abc, container, false);
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();

// PERFORM YOUR OPERATION OVER HERE
    }
}

Let me know if this works for you ! :)

Vivek Bhardwaj
  • 530
  • 5
  • 16
0

In your fragment write below code and overide onActivityResult(int requestCode, int resultCode, Intent data) method

            Intent intent = new  Intent(getApplicationContext(),TrendingQuestions.class);
        intent.putExtra("categoryId", Integer.parseInt(categoryId));
        startActivityForResult(intent, 101);

In your next activity after you got the result just set result

Intent result = new Intent();
setResult(Activity.RESULT_OK, result);
finish();

If onActivityResult is not getting called please check this link

Community
  • 1
  • 1
Prashanth Debbadwar
  • 1,047
  • 18
  • 33
0

Then do one thing .. use the Activities onResume Method in which you are showing the Fragment and then from within this Activity's OnResume call the Fragment' Function where you want to refresh the Fragment.

For example. You have a Activity_A in which you have defined the fragment, let it be Fragemtn_A. Now you are navigating to Activity_B from within this Fragment_A.

Now when you are leaving Activity_B, then the onResume() method of the Activity_A will be called for sure, and from the onResume() of Activity_A you can call the function of Fragment_A and perform your operations that you want.

For calling any fragment's function from withing the Activity you can follow this link : Calling a Fragment method from a parent Activity

Let me know if this works for you! :)

Community
  • 1
  • 1
Vivek Bhardwaj
  • 530
  • 5
  • 16