Android片段生命周期:onResume调用两次

时间:2017-05-17 12:04:14

标签: android android-activity fragment lifecycle onresume

我试图在用户切换到另一个活动后保存并恢复我的片段中的滚动,然后返回到当前的活动。 这是发生的事情: enter image description here

正如你可以看到,在第一个片段的onResume继续onPause之后,虽然用户没有做任何事情。

问题是:我的代码出了什么问题?

这是我的代码:

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();
}}

片段代码:

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 个答案:

答案 0 :(得分:3)

Fragment正在被替换两次,这就是为什么onResume of Fragment被调用两次的原因。你应该只从活动的onCreate()调用你的RefreshFragment(),而不需要从活动的onResume()调用它。

答案 1 :(得分:1)

首先,我认为您应该检查onResume()中的DisplayNoteActivity是否也被调用过两次?如果是这样的话, 1.检查requestPermission()在启动期间是否被调用。 2.检查DisplayNoteActivity是否未得到处理。

如果onConfigurationChanged()中的onResume()仅被调用一次,则检查DisplayNoteActivity出了什么问题。