简体   繁体   English

从片段失败开始Intent活动-Android

[英]Start Intent activity from fragment fail - android

I want to start the activity from fragment but fail. 我想从片段开始活动,但失败。

Here is my code 这是我的代码

MainActivity myactivity = (MainActivity) getActivity();


Intent intent = new Intent(myactivity, PopUpImageActivity.class);
Bundle extras = new Bundle();
myactivity.startMyIntent(intent);

尝试这个:

    startActivity(new Intent(getActivity().getApplicationContext(), YourActivity.class));

Instead of doing the intent from your activity , why not doing the intent on the fragment class itself? 除了从您的activity中进行intent ,为什么不对fragment类本身进行intent

For example, 例如,

Intent intent = new Intent(getActivity(), PopUpImageActivity.class);
getActivity().startActivity(intent);

This should work and there's no need to access your activity because all fragment resides on an activity anyway and it already knows by itself which activity it is residing without having it specified 这应该可以工作,并且不需要访问您的activity因为所有fragment仍然驻留在一个activity ,并且它本身已经知道了要驻留的activity ,而没有指定它

The answer above may/maynot be right but for the best practice always allow transactions through the Activity which enclose the respective Fragment . 上面的答案可能是正确的,也可能不是正确的,但是为了最佳实践,总是允许通过包含相应 FragmentActivity进行交易。

Make use of interfaces , onAttach () in your Fragment for giving call to the enclosing Activity & from the activity, allow for another activity/fragment transaction. 利用Fragment中interface onAttach ()来调用封闭的Activity并从该活动调用该活动,并允许进行另一个活动/片段事务。

A sample code would look like this: 示例代码如下所示:

public class MyFragmentExample extends Fragment{

//Creating instance of Interface
AnyInterfaceName anyInterfaceName;


/*
This onAttach is responsible for attaching the interface
listener of fragment to Activity
*/
 @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        try {
            anyInterfaceName = (AnyInterfaceName) context;
        } catch (ClassCastException ex) {
            throw new ClassCastException(ex.getMessage() + "must implement AnyInterfaceName");
        }
    }

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return view;
    }


// Define an interface with any name
public interface AnyInterfaceName{
    /*
      you can give any name to this function & this
      function will be implemented later in activity
    */
    void startAnotherActivity();
}
}

Now on the Activity side: add this on top just after extends Activity/AppCompActivity 现在在“活动”端:在扩展Activity / AppCompActivity之后, 将其添加到顶部

implement MyFragment.AnyInterfaceName 实现MyFragment.AnyInterfaceName

After this, just implement the method & inside the method, allow transiting to Another Activity as you do it from Activity using Intent . 之后,只需实现方法方法内部,就可以使用Intent从Activity转移到Another Activity。

Note: Don't forget to call to the interface in fragment while you want for starting another Activity. 注意:当您要启动另一个Activity时, 不要忘记分段调用该接口。 Simply call it like this: 只需这样称呼它:

anyInterfaceName.startAnotherActivity(); anyInterfaceName.startAnotherActivity();

It can be a lengthy work to do but for the best practice, it is. 这可能是一项漫长的工作,但对于最佳实践而言,确实如此。

Check out from official android site Communicating with the Activity here 从Android官方网站与活动沟通看看这里

Hope it helps !! 希望能帮助到你 !!

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

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