Android fragment lifecycle: onResume called twice

Viewed 8076

I am trying to save and restore scrolling in my fragment after user switches to another activity and then returns to the current one. Here is what happens: enter image description here

As you can see straight after first fragment's onResume goes onPause, although user does not do anything.

The question is : What is wrong with my code?

Here is my code:

public class DisplayNoteActivity extends Activity {

private Context mContext;
//static final int SUBACT_EDITNOTE=100;
private long m_NoteID;
private String m_sTextNoAbbrev;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    try
    {
        Log.v("DEBUG", "DisplayNoteActivity.onCreate()");
        setContentView(R.layout.activity_display_note);

        Bundle extras = getIntent().getExtras();
        m_NoteID = extras.getLong("DisplayNote_NOTEID");
        m_sTextNoAbbrev = extras.getString("DisplayNote_TextNoAbbrev");

        if (savedInstanceState != null)
            RefreshFragment();
    }
    catch(Exception e){
        Toast.makeText(this, e.getMessage() , Toast.LENGTH_LONG).show();
        e.printStackTrace();
    }

}


public void onSaveInstanceState(Bundle savedInstanceState)
{
    Log.v("DEBUG", "DisplayNoteActivity.onSaveInstanceState()");
    super.onSaveInstanceState(savedInstanceState);
    savedInstanceState.putLong("DisplayNote_NOTEID", m_NoteID);
    savedInstanceState.putString("DisplayNote_TextNoAbbrev", m_sTextNoAbbrev);
}

public void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    // Restore UI state from the savedInstanceState.
    // This bundle has also been passed to onCreate.
    Log.v("DEBUG", "DisplayNoteActivity.onRestoreInstanceState()");
    m_NoteID = savedInstanceState.getLong("DisplayNote_NOTEID");
    m_sTextNoAbbrev= savedInstanceState.getString("DisplayNote_TextNoAbbrev");
}

@Override
public void onResume() {
    Log.v("DEBUG", "DisplayNoteActivity.onResume()");
    super.onResume();

    RefreshFragment();
}


private void RefreshFragment()
{

    Bundle bundle = new Bundle();
    bundle.putLong("DisplayNote_NOTEID", m_NoteID);
    bundle.putString("DisplayNote_TextNoAbbrev", m_sTextNoAbbrev);

    android.app.FragmentManager fm=getFragmentManager();
    android.app.FragmentTransaction ft = fm.beginTransaction();

    DisplayNoteFragment dnf = new DisplayNoteFragment();
    dnf.setArguments(bundle);
    ft.replace(R.id.container, dnf);

    ft.commit();
}}

Fragment code:

public class DisplayNoteFragment extends Fragment {


private Context mContext;

private long mNoteID = 0;
private Note m_NoteData;

private long mTextID = 0;
private int m_levels = 0;

private String m_dbtype;
private String m_dblang;
GitaTextsHelper m_tdb;

private String m_sTextNo;
private ScrollView m_Scroll;
private int mScrollPos=0;

AutoResizeTextView m_atvHeaderTitle;
AutoResizeTextView m_atvHeaderSubTitle;

AutoResizeTextView m_atvBookTitle;
AutoResizeTextView m_atvChapterTitle;
TextView m_atvTags;
AutoResizeTextView m_atvVerse;

TextView m_tvGitaText;
ImageView m_NoteTypeIcon;
ImageView m_EditNoteIcon;
TextView m_tvNote;

int m_titlePrefixID;


long m_TextRowID = -1;
String m_sVerse;

public DisplayNoteFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_display_note, container, false);
    Log.v("DEBUG", "DisplayNoteFragment onCreateView()");
    try {
        m_Scroll = (ScrollView)rootView.findViewById(R.id.ScrollView02);
        m_atvHeaderTitle = (AutoResizeTextView) rootView.findViewById(R.id.header_title);
        m_atvHeaderSubTitle = (AutoResizeTextView) rootView.findViewById(R.id.header_subtitle);

        m_atvBookTitle = (AutoResizeTextView) rootView.findViewById(R.id.book_title);
        m_atvChapterTitle = (AutoResizeTextView) rootView.findViewById(R.id.chapter_title);
        m_atvTags = (TextView) rootView.findViewById(R.id.note_tags);
        m_atvVerse = (AutoResizeTextView) rootView.findViewById(R.id.note_verse);

        m_tvGitaText = (TextView) rootView.findViewById(R.id.gita_text);
        m_tvNote = (TextView) rootView.findViewById(R.id.note_text);

        //***** INPUT
        mNoteID = getArguments().getLong("DisplayNote_NOTEID");
        m_NoteData = MyApp.mUserDB.GetNote(mNoteID);
        // common fields for text notes and custom notes
        m_atvHeaderSubTitle.setText(m_NoteData.sCreatedDate);
        m_NoteTypeIcon = (ImageView) rootView.findViewById(R.id.note_type_icon); m_NoteTypeIcon.setImageResource(MyApp.getNoteTypeIcon(m_NoteData.type));
        //tags
        String tags = MyApp.mUserDB.getNoteTags(mNoteID);

        if (tags.equals("")) tags = MyApp.Res.getString(R.string.no_tag_defined);
        m_atvTags.setText(tags);
        m_atvTags.setPaintFlags(m_atvTags.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
        m_atvTags.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if (MyApp.mC.hasFlag(Cookies.FL_Tagging, mContext, true)) {
                    Intent intent = new Intent(mContext, TagEdit.class);
                    intent.putExtra("TAGEDIT_NOTEID", mNoteID);
                    intent.putExtra("TAGEDIT_EDIT_TAGS_MODE", 1);

                    startActivity(intent);
                }

            }
        });


    } catch (Exception e) {
        Toast.makeText(mContext, e.getMessage(), Toast.LENGTH_LONG).show();
        e.printStackTrace();
    }

    return rootView;
}

private void openText(int iScroll) {

    Intent intent = new Intent(mContext, DisplayText.class);
    intent.putExtra("rowID", m_TextRowID);
    intent.putExtra("Scroll", iScroll);
    intent.putExtra("Language", m_dblang);
    intent.putExtra("DBType", m_dbtype);
    startActivity(intent);
}


public void onPause() {
    super.onPause();

    mScrollPos=m_Scroll.getScrollY();
    Log.v("DEBUG", "DisplayNoteFragment onPause(). Scroll="+mScrollPos);
}


public void onResume() {
    Log.v("DEBUG", "DisplayNoteFragment onResume(). Scroll="+mScrollPos);
    super.onResume();
    if(mScrollPos!=0) {
        m_Scroll.scrollTo(0, mScrollPos);
        Log.v("DEBUG", "DisplayNoteFragment onResume(). Scrolled to="+mScrollPos);
    }

}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    if(outState==null) return;
    Log.v("DEBUG", "DisplayNoteFragment onSaveInstanceState(). Scroll="+mScrollPos);
    outState.putInt("mScrollPos", mScrollPos);

}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    if(savedInstanceState==null) return;
    mScrollPos = savedInstanceState.getInt("mScrollPos");
    Log.v("DEBUG", "DisplayNoteFragment onActivityCreated(). Scroll="+mScrollPos);
}

}

2 Answers

Firstly, I think you should check if onResume() of DisplayNoteActivity is called twice too? If so, 1. check if requestPermission() is invoked during DisplayNoteActivity launched. 2. check if onConfigurationChanged() is not being handling.

If onResume() of DisplayNoteActivity is called only once, then check what's wrong with DisplayNoteFragment.

Related