简体   繁体   English

从片段启动活动

[英]Launching Activity from Fragment

I have an Activity with a single Fragment that asks the user to login. 我有一个带有单个Fragment的Activity,要求用户登录。 Once the person logs in, a new Activity is launched. 该人登录后,将启动一个新的活动。 My question is, once the the person enters their credentials and hits the login button on the Fragment should 我的问题是,一旦该人输入了凭据并点击了Fragment上的登录按钮,

A)the fragment alert its current Activity first and then from there start the new Activity. A)片段首先警告其当前活动,然后从那里开始新的活动。 For example, here is my Fragment: 例如,这是我的片段:

public class LoginFragment extends Fragment implements View.OnClickListener {
private Button loginButton;
private ClickedInterface clickedInterface;

public LoginFragment() {
    // Required empty public constructor
}

static interface ClickedInterface{
    public void buttonClicked(View v);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_login, container, false);
    loginButton = (Button)view.findViewById(R.id.fragment_login_loginButton);
    loginButton.setOnClickListener(this);
    return view;
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    this.clickedInterface = (ClickedInterface)activity;
}

@Override
public void onDetach() {
    super.onDetach();
    clickedInterface = null;
}

@Override
public void onClick(View v) {
    switch (v.getId()){
        case R.id.fragment_login_loginButton:{
            clickedInterface.buttonClicked(v);
            break;
        }
    }
}

And Here is the Activity using the ClickedInterface method: 这是使用ClickedInterface方法的Activity:

    @Override
public void buttonClicked(View v) {
    switch (v.getId()){
        case R.id.fragment_login_loginButton:{
            //Do Stuff
            break;
        }
    }
}

OR 要么

B)launch the new Activity right from the Fragment? B)直接从片段启动新的活动?

Thank you 谢谢

It totally depends on what business logic you have in your fragment, and whether you're using it in multiple activities. 这完全取决于您的片段中包含哪些业务逻辑,以及是否在多个活动中使用它。 For example, you might have a share button, and a button that sends an Intent to rate your app in the Play Store. 例如,您可能有一个共享按钮和一个发送Intent以便在Play商店中为您的应用评分的按钮。

In the sharing scenario, you might want each activity to have a different share text. 在共享方案中,您可能希望每个活动都有不同的共享文本。 In this case, you would get a reference of the activity, check that it implements an interface, then delegate everything to the activity: 在这种情况下,您将获得活动的引用,检查该活动是否实现了一个接口,然后将所有内容委派给该活动:

// share scenario (delegate to Activity)

if (getActivity() instanceof MyCallback) {
    ((MyCallback)getActivity()).launchMyIntent(); // TODO handle callback in activity
}

However, if you know for a fact that you only want one behaviour regardless of where you are in the app (like with a rating button), it might make sense to just send your intent straight from the Fragment. 但是,如果您知道一个事实,即无论您在应用程序中的位置如何,都只想要一种行为(例如带有评级按钮),则直接从Fragment发送意图即可。

// rating scenario (send intent from fragment

getActivity().startActivity(myIntent);

The best way is to pass the control to MainActivity and then let the activity do the rest. 最好的方法是将控件传递给MainActivity,然后让活动完成其余的工作。

Second way of doing is--> Create a static method like openPostLoginActivity inside MainActivity and call it from your fragment. 第二种方法是->在MainActivity内部创建一个类似于openPostLoginActivity的静态方法,然后从您的片段中调用它。 This way you can ensure that the global action is always residing in parent. 这样,您可以确保全局操作始终驻留在父级中。 This would mostly help you when you will be having multiple fragments. 当您有多个片段时,这将对您大有帮助。 I have seen this type of approach used by pubnub sample android app 我已经看到pubnub示例android应用程序使用的这种方法

Third is the one which you mentioned. 第三是您提到的那个。 Opening it from fragment itself. 从片段本身打开它。

Frankly speaking in your scenario you can use any one of above. 坦白地说,在您的方案中,您可以使用以上任何一种。 It depends whether you want to stuck with the standards or just want your work to get done. 这取决于您是坚持标准还是只想完成工作。 All the best!!1 祝一切顺利!1

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

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