如何在mainactivity中调用fragmentactivity(AppCompactActivity)

时间:2016-06-03 10:23:12

标签: android android-fragments

如何在MainActivity中访问FragmentActivity(AppCompactActivity)

这是我的FragmentActivity:

public class MainFragment extends FragmentActivity {

private Fragment contentFragment;
HomeFragment homeFragment;
ProductDetailFragment pdtDetailFragment;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    FragmentManager fragmentManager = getSupportFragmentManager();

    if (savedInstanceState != null) {
        if (savedInstanceState.containsKey("content")) {
            String content = savedInstanceState.getString("content");
            if (content.equals(ProductDetailFragment.ARG_ITEM_ID)) {
                         if(fragmentManager.findFragmentByTag(ProductDetailFragment.ARG_ITEM_ID) != null) {
                    contentFragment = fragmentManager
                            .findFragmentByTag(ProductDetailFragment.ARG_ITEM_ID);
                }
            }
        }
        if (fragmentManager.findFragmentByTag(HomeFragment.ARG_ITEM_ID) != null) {
            homeFragment = (HomeFragment) fragmentManager
                    .findFragmentByTag(HomeFragment.ARG_ITEM_ID);
            contentFragment = homeFragment;
        }
        } else {
        homeFragment = new HomeFragment();
        switchContent(homeFragment, HomeFragment.ARG_ITEM_ID);
        }
        } 

以下是扩展AppcompactActivity的My MainActivity:

 public class MainActivity extends AppCompatActivity {

     //How to i use FragmentActivity in MainActivity
 }

2 个答案:

答案 0 :(得分:1)

使用此代码重播您的代码。

片段:代码。

public class MainFragment extends Fragment{

 private Fragment contentFragment;
 HomeFragment homeFragment;
ProductDetailFragment pdtDetailFragment;
View rootView;
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    rootView = inflater.inflate(R.layout.fragment_share_trip, container, false);
    try {
    FragmentManager fragmentManager = getSupportFragmentManager();

if (savedInstanceState != null) {
    if (savedInstanceState.containsKey("content")) {
        String content = savedInstanceState.getString("content");
        if (content.equals(ProductDetailFragment.ARG_ITEM_ID)) {
                     if(fragmentManager.findFragmentByTag(ProductDetailFragment.ARG_ITEM_ID) != null) {
                contentFragment = fragmentManager
                        .findFragmentByTag(ProductDetailFragment.ARG_ITEM_ID);
            }
        }
    }
    if (fragmentManager.findFragmentByTag(HomeFragment.ARG_ITEM_ID) != null) {
        homeFragment = (HomeFragment) fragmentManager
                .findFragmentByTag(HomeFragment.ARG_ITEM_ID);
        contentFragment = homeFragment;
    }
    } else {
    homeFragment = new HomeFragment();
    switchContent(homeFragment, HomeFragment.ARG_ITEM_ID);
    }
    }
    catch (Exception ex) {

    }
    return rootView;
    }`

MainActivity Code:

        public class MainActivity extends AppCompatActivity {

 MainFragment fragment2 = new MainFragment();
            android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.content_frame, fragment2);
            fragmentTransaction.commit();
    }

答案 1 :(得分:0)

Fragment Activity是自我Activity

您无法在FragmentActivity内拨打Activity

如果您想使用FragmentActivity,则可以延长FragmentActivity而不是Activity

或者您只能在Fragment内拨打Activity