android-fragments 通过片段中的Intent启动片段

mm5n2pyu  于 2022-11-14  发布在  Android
关注(0)|答案(5)|浏览(217)

I want to launch a new fragment to view some data. Currently, I have a main activity that has a bunch of actionbar tabs, each of which is a fragment. So, within a tab fragment, I have a button, chartsButton. I have my onclicklistener all set for it, and here's the onClick method:

public OnClickListener chartsListener = new OnClickListener() {

    @Override
    public void onClick(View v) {
        Intent chartFragment = new Intent();
        startActivity(chartFragment);   
    }
};

Now, as I said, this listener is within a class that extends Fragment. So, I want to launch a new fragment (chartsFragment) via the intent to replace the whole screen. When the user clicks back, it'll bring them back to the tabs and main activity. Here's my chart fragment:

public class chartsFragment extends Fragment {

    public View onCreateView() {
        //LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState
        LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        return (inflater.inflate(R.layout.chartfragment, null));
    }
}

The current error I am dealing with: "android.content.ActivityNotFoundException: No Activity found to handle Intent { }". That's fine, I understand that I could use getActivity().startActivity(chartsFragment), but that results in the same error. I suppose what I am looking for here, is how do I launch an intent from within a fragment that results in opening a new fragment?

c0vxltue

c0vxltue1#

你的问题的答案很简单:使用新的Fragment替换当前的Fragment并将事务推送到后备堆栈。这将保留“后退”按钮的行为...
创建一个新的Activity确实挫败了使用片段的整个目的无论如何...非常适得其反。

@Override
public void onClick(View v) {
    // Create new fragment and transaction
    Fragment newFragment = new chartsFragment(); 
    // consider using Java coding conventions (upper first char class names!!!)
    FragmentTransaction transaction = getFragmentManager().beginTransaction();

    // Replace whatever is in the fragment_container view with this fragment,
    // and add the transaction to the back stack
    transaction.replace(R.id.fragment_container, newFragment);
    transaction.addToBackStack(null);

    // Commit the transaction
    transaction.commit(); 
}

http://developer.android.com/guide/components/fragments.html#Transactions

ycggw6v2

ycggw6v22#

您无法打开新片段。片段需要始终由Activity托管。如果片段位于同一Activity中(例如标签页),那么后退键导航将很棘手。我假设您想用该片段打开一个新屏幕。
因此,您只需创建一个新Activity,并将新片段放入其中,然后Activity将通过Activity类显式或通过intent filter s隐式地对Intent做出React。

y1aodyip

y1aodyip3#

试试这个它可能会帮助你:

public void ButtonClick(View view) {
    Fragment mFragment = new YourNextFragment(); 
    getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, mFragment).commit();
}
jogvjijk

jogvjijk4#

试试这个它可能会帮助你:

private void changeFragment(Fragment targetFragment){

    getSupportFragmentManager()
         .beginTransaction()
         .replace(R.id.main_fragment, targetFragment, "fragment")
         .setTransitionStyle(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
         .commit();

}
syqv5f0l

syqv5f0l5#

试试这个:

在第二个活动中:

btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(this, FirstActivity.class);
            intent.putExtra("openFragment", "YourFragment");
            startActivity(intent);
        }
    });

在包含该片段的FisrtActivity中:

if (getIntent().getStringExtra("openFragment").equals("YourFragment")){
            getSupportFragmentManager().beginTransaction().replace(R.id.containerView, new YourFragment()).commit();
        }

相关问题