还是来请教大牛吧,关于activity传数据到fragment中的问题

我有一个MainActivity,里面有三个fragment,然后第三个fragment中有登录按钮跳转到LoginMainActivity,我想要实现当点击LoginActivity中的登录按钮返回,并且把LoginActivity中的name传到Fragment中的TextView;
下面是MainAtivity中Fragment的代码
package textview.exam;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

public class FragmentMe extends Fragment{
protected String result;
Button loginbutton;
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);

}

@Override
public View onCreateView(LayoutInflater inflater,
                         @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    View view=inflater.inflate(R.layout.main_me,container,false);
    loginbutton=(Button)view.findViewById(R.id.login);
    loginbutton.setOnClickListener(startLoginListener);
    return view;
}
View.OnClickListener startLoginListener=new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent intent=new Intent(getActivity(), LoginActivity.class);
        startActivity(intent);
        getActivity().overridePendingTransition(R.anim.push_down_in, R.anim.push_down_out);

    }

};

}

下面是LoginActivity的代码
package textview.exam;

import android.app.ActionBar;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import java.util.ArrayList;
import java.util.List;

public class LoginActivity extends Activity {
Button okbutton;
String name;
String pass;

HttpClient httpClient;
Handler handler = new Handler()
{
    public void handleMessage(Message msg)
    {
        if(msg.what == 0x123)
        {


        }
    }
};
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.login);
    final ActionBar actionBar = getActionBar();
    actionBar.hide();
    httpClient=new DefaultHttpClient();
    okbutton=(Button)findViewById(R.id.ok);
    okbutton.setOnClickListener(loginokListener);
    Button loginback=(Button)findViewById(R.id.backlogin);
    loginback.setOnClickListener(backloginListener);

}
View.OnClickListener backloginListener=new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        finish();
    }
};
View.OnClickListener loginokListener=new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        // 获取用户输入的用户名、密码
        name = ((EditText)findViewById(R.id.login_name)).getText().toString();
        pass = ((EditText)findViewById(R.id.login_password)).getText().toString();
        new Thread()
        {
            @Override
            public void run()
            {
                try
                {
                    HttpPost post = new HttpPost("http://192.168.100.101:8080/foo/login.jsp");//③
                    // 如果传递参数个数比较多,可以对传递的参数进行封装
                    List<NameValuePair> params = new ArrayList<>();
                    params.add(new BasicNameValuePair
                            ("name", name));
                    params.add(new BasicNameValuePair
                            ("pass", pass));
                    // 设置请求参数
                    post.setEntity(new UrlEncodedFormEntity(
                            params, HTTP.UTF_8));
                    // 发送POST请求
                    HttpResponse response = httpClient.execute(post);  //④
                    // 如果服务器成功地返回响应
                    if (response.getStatusLine()
                            .getStatusCode() == 200)
                    {
                        String msg = EntityUtils
                                .toString(response.getEntity());
                        Log.i("wolaoadmasmdas"+msg,"");
                        Looper.prepare();
                            Toast.makeText(LoginActivity.this,
                                    msg, Toast.LENGTH_LONG).show();
                            Looper.loop();
                    }
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }
            }
        }.start();
    }
};
//返回动画
protected void onPause() {
    super.onPause();
    overridePendingTransition(R.anim.in_from_down,
            R.anim.out_to_up);
}

}

3个回答

我自己查书解决了,具体步骤是在MainActivty的Fragemt中用startActivityForResult启动Loginactivity,然后在loginactivity中写 Intent in = getIntent();
in.putExtra( "result", uname );
LoginActivity.this.setResult(0, in);
LoginActivity.this.finish();
然后在Mainactivity中的Fragment中调用 @Override
public void onActivityResult(int requestCode, int resultCode, Intent intent)
{
if(requestCode==0&&resultCode==0){
Bundle data=intent.getExtras();
String name=data.getString("result");
main_me_loginsuccess.setText(name);
loginbutton.setText("退出");
loginbutton.setOnClickListener(stopLoginListener);

    }


}

跳转loginactivity的时候用startactivityforresult 然后在mainactiivty里面获取,通过bundle传给fragment

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问