Activity fragment the value of another activity

Source: Internet
Author: User
Tags gettext stub

List Contents

In the study and project use will use the activity to think of another activity in the fragment value, on the Internet to read a lot of senior posts, their own summary write a value-based way. For example, in a shopping mall app, the main interface nested 4fragment, there is a personal center, click the Login button of the personal center to jump to the login screen, in the login screen user name sent back to the personal center, there are many places will be used, of course, there are many ways to solve, For example, the login information is stored directly in the sharedpreferences and then to fragment to judge, can also be used Startactivityforresult.
Here is about using Startactivityforresult to pass the value, directly on the code

This is the main interface, the main interface nesting fragment, the code is very simple is bound fragment

Package Com.demo.actyfrafmrntdemo;
Import android.app.Activity;
Import Android.os.Bundle;

public class Mainactivity extends Activity {
    private onefragment onefragment;

    @Override
    protected void onCreate (Bundle savedinstancestate) {
        super.oncreate (savedinstancestate);
        Setcontentview (r.layout.activity_main);
        Onefragment=new onefragment ();
        Chained programming style
        Getfragmentmanager (). BeginTransaction (). Add (R.id.fragment, onefragment). commit ();
    }
}

Next is the fragment operation, here is a TextView and a Button,button click to jump to the login screen, and then the user name of the login interface to the TextView, the layout file is not posted

Package Com.demo.actyfrafmrntdemo;
Import android.app.Fragment;
Import android.content.Intent;
Import Android.os.Bundle;

Import android.support.annotation.Nullable;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.view.ViewGroup;
Import Android.widget.Button;

Import Android.widget.TextView;
    public class Onefragment extends Fragment {private view view;
    Private Button Mbutton;

    Private TextView Mttextview; @Override @Nullable public View oncreateview (Layoutinflater inflater, @Nullable viewgroup container, @ Nullable Bundle savedinstancestate) {//TODO auto-generated method stub return view = Inflater.inflate (R.
    Layout.fragment_layout, NULL); 
        } @Override public void onactivitycreated (Bundle savedinstancestate) {//TODO auto-generated method stub
        Super.onactivitycreated (savedinstancestate); Mbutton = (Button) View.findviewbyid (R.ID.BTN);
        Mttextview = (TextView) View.findviewbyid (R.id.text_view);
                Mbutton.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) {

            Startactivityforresult (New Intent (Getactivity (), Logingacty.class), 1);
    }
        }); }/** * This method is used to jump the data that the activity is going to send back, and to receive the data to be sent back to do the business * here because there is only one activity return data, so there is only one resultcode, directly receive the return value processing * /@Override public void Onactivityresult (int requestcode, int resultcode, Intent data) {//TODO Auto-gene
        Rated method Stub Super.onactivityresult (Requestcode, ResultCode, data);
        Bundle bundle = Data.getextras ();
        String namstring = bundle.getstring ("name");
    Mttextview.settext (namstring);
 }

}

The next step is to return the activity of the data

Package Com.demo.actyfrafmrntdemo;
Import android.app.Activity;
Import android.content.Intent;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
Import Android.widget.EditText;

Import Android.widget.TextView;
    public class Logingacty extends activity{private EditText nameview;
    Private EditText Pwdtextview;
    Private Button Mbutton; @Override protected void OnCreate (Bundle savedinstancestate) {//TODO auto-generated method stub Supe
        R.oncreate (savedinstancestate);
        Setcontentview (r.layout.loging_activity);
        nameview= (EditText) Findviewbyid (r.id.name);
        pwdtextview= (EditText) Findviewbyid (R.ID.PWD);
        mbutton= (Button) Findviewbyid (R.id.brn_login);
                Mbutton.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) { TODO auto-generated Method Stub String Namestring=namEview.gettext (). toString ();
                String Pwd=pwdtextview.gettext (). toString ();
                Intent intent=new Intent ();
                Bundle Bundle=new Bundle ();
                Bundle.putstring ("name", namestring);
                Bundle.putstring ("pwd", PWD);
                Intent.putextras (bundle);
                1 is the return of the Requestcode LogingActy.this.setResult (1,intent);



            It's going to kill the finish () directly.
    }
        });
 }

}

The

about Startactivityforresult is here to pass the value. Android Novice, there is wrong to the wrong place please advise

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.