简体   繁体   English

尽管存在片段事务的commit(),但未调用onAttach()-方法

[英]onAttach()-method is not being called, although there is a commit() of the fragment transaction

I am wondering, how I can figure out, why my onAttach-method is not getting called. 我想知道如何确定为什么我的onAttach方法没有被调用。 There is a commit of the fragment transaction and a corresponding replace. 提交了片段事务并进行了相应的替换。 My log-Messages and my debug steps are telling me, that the onAttach() method of my QuotesFragment class never got triggered. 我的日志消息和调试步骤告诉我, QuotesFragment类的onAttach()方法从未触发过。

Here is my forward procedure in my MainActivity-class: 这是我的MainActivity类中的前进过程:

private void forwardToQuotesFragment(){
    FragmentTransaction ft = getFragmentManager().beginTransaction();
    ft.replace(R.id.container, quotesFragment, QuotesFragment.QUOTES_FRAGMENT_TAG);
    ft.commit();
    // onAttach()-method of QuotesFragment is not getting called - why?
}

I found the problem. 我发现了问题。 As I mentioned in my prior comment, the reason for my issue was that on my cell phone Android 5.0.2 was installed and my app was created for Android 6.0 upwards. 正如我在先前的评论中提到的那样,出现此问题的原因是在手机上安装了Android 5.0.2,并且为Android 6.0以上版本创建了我的应用。

Furthermore I used the android.app.*-classes. 此外,我使用了android.app。*类。 Now I changed those classes to the corresponding android-support-library-classes. 现在,我将这些类更改为相应的android-support-library-classs。 That means my MainActivity extends from AppCompatActivity and my fragments from android.support.v4.app.Fragment. 这意味着我的MainActivity来自AppCompatActivity,而我的片段则来自android.support.v4.app.Fragment。 There is a need to update the fragment transaction procedure a little bit, as you can see below: 您需要稍微更新片段事务过程,如下所示:

private void forwardToQuotesFragment(){
    android.support.v4.app.FragmentManager fm = getSupportFragmentManager();
    android.support.v4.app.FragmentTransaction ft = fm.beginTransaction();
    ft.replace(R.id.container, quotesFragment);
    ft.commit();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM