0

I have fragment called view_profile and activity called edit_profile After editing the profile if user press on back button then I want to get updated content.

Right now, on back button pressed it simply loads previous fragment without reloading the content. How can I restart the fragment activity?

I searched on internet. I found method called onRestart. But, I think it's the method for activity not the fragment.

RNK
  • 5,582
  • 11
  • 65
  • 133
  • why aren't you using the principe of communicating between fragments, or a mecanism like "startActivityForResult()" between your two fragments – Houcine Jan 09 '15 at 16:58

2 Answers2

0

You can override activity's onBackPressed() and implement your own logic there.

Dara
  • 107
  • 1
  • 7
-1

In view_profile, use startActivityForResult to start edit_profile activity. In edit_profile, handle the back button event to update the content:

@Override
public boolean onOptionsItemSelected(MenuItem item)
{
    switch ( item.getItemId() )
    {
        case android.R.id.home:
        {
            // update the content
            this.getIntent().putExtra( "CONTENT_PARCELABLE", (Parcelable)content_data );
            this.getIntent().putExtra( "CONTENT_STRING", (String)string_content_data );
            setResult( RESULT_OK, this.getIntent() );

            finish();
            return true;
        }
    }
}

...

in view_profile, get the updated result from data

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{
    boolean  have_changes = false;

    if ( requestCode == REQUEST_RECORD_EDIT )
    {
        if ( resultCode == RESULT_OK )
        {
            // update the content from data
            updated_content = data.getParcelableExtra("CONTENT_PARCELABLE");
            updated_string_content = data.getStringExtra("CONTENT_STRING");
        }
    }
}
Flanker
  • 408
  • 3
  • 7
  • That's what I am doing right now. It won't reload the content – RNK Jan 09 '15 at 16:57
  • I just want to recall the `onCreateView` method again – RNK Jan 09 '15 at 16:58
  • Maybe you should declare "global" variable accessible from both activities, extending Application: http://stackoverflow.com/questions/708012/how-to-declare-global-variables-in-android/ – Flanker Jan 09 '15 at 17:26
  • Or consider singleton pattern - http://stackoverflow.com/questions/3826905/singletons-vs-application-context-in-android – Flanker Jan 09 '15 at 17:31