[英]How to access an object from an activity into a fragment?
我想將對象從活動傳遞到片段。 我的活動具有一個由視圖分頁器和四個片段組成的選項卡布局。
我在活動中有一個對象,我想在所有片段中對其進行訪問。
我試圖在活動中創建一個getMethod,然后分段訪問該對象。 但是,在將對象作為null的所有方式上,它都無法正常工作。
活動:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("Profile");
toolbar.setNavigationIcon(R.drawable.ic_arrow_back_white_24dp);
setSupportActionBar(toolbar);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
GetProfileAsyncTask task = new GetProfileAsyncTask(ProfileActivity.this,this,profile);
// task.setTaskCompletedListener(this);
task.execute("sid");
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager(),mTitles,mNumbOfTabs);
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager)findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
mTabs = (SlidingTabLayout)findViewById(R.id.tabs);
mTabs.setDistributeEvenly(true);
mTabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
@Override
public int getIndicatorColor(int position) {
return getResources().getColor(R.color.tab_scroll_color);
}
});
mTabs.setViewPager( mViewPager);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public JSONObject getProfile(){
return this.profile;
}
@Override
public void doInBackground() {
}
@Override
public void doPostExecute(JSONObject response) {
profile = response;
Toast.makeText(ProfileActivity.this,
"Finish", Toast.LENGTH_LONG).show();
}
我想將配置文件對象傳遞給片段。
分段:
public class BasicInformationFragment extends android.support.v4.app.Fragment{
EditText email,pass,mobile,code;
public static final String MyPREFERENCES = "MyPrefs" ;
SharedPreferences sharedpreferences;
JSONObject profile;
String userUsername,password,mobileNumber,emailId;
private ProfileActivity myActivity;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_basic_information,
container, false);
// try{
((ProfileActivity)getActivity()).getProfile();
// password = profile.getString("userPassword");
// mobileNumber = profile.getString("userMobileNo");
// emailId = profile.getString("userEmailid");
email = (EditText) view.findViewById(R.id.edt_email);
pass = (EditText) view.findViewById(R.id.edt_pass);
mobile = (EditText) view.findViewById(R.id.edt_mobile);
code = (EditText) view.findViewById(R.id.edt_code);
// email.setText(emailId);
// pass.setText(password);
// mobile.setText(mobileNumber);
// }
// catch (JSONException e)
// {}
return view;
}
這是怎么了? 請幫忙。 謝謝..
基本上我想訪問Fragment中的Activity對象。 如果還有其他方法,請告訴我。 謝謝
在一段時間內重寫onAttched()方法的給定活動對象getActivity()方法給出活動的空引用。
public void onAttach(Activity activity) {
super.onAttach(activity);
this.mActivity = activity;
JSONObject mObject =((ProfileActivity)getActivity()).getProfile();
}
為JSON對象創建一個單獨的通用類。並為其自身的主要活動分配值。即,
public class Common{
public static JSONObject commonprofileobject;
}
然后從活動分配值Common.commonprofileobject=XXXX;
然后你可以從片段訪問它
為什么在Activity中運行異步任務。 意味着您也可以在Fragment中運行異步任務。 並從片段中的回調獲取結果。
為此,在您的項目中添加一個basefragment類,並從basefragment類擴展您的片段
public class BaseFragment extends Fragment{
private Activity activity;
public ProgressDialog prog = null;
private MainActivity parent;
private final String TAG = "";//MainActivity.class.getName().toString();
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
this.activity = activity;
setParent(getHomeActivity());
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return super.onCreateView(inflater, container, savedInstanceState);
}
@Override
public void onPause() {
super.onPause();
}
@Override
public void onResume() {
super.onResume();
}
@Override
public void onStart() {
super.onStart();
}
@Override
public void onStop() {
super.onStop();
}
public MainActivity getHomeActivity() {
try {
return (MainActivity) activity;
} catch (Exception ex) {
return null;
}
}
public MainActivity getParent() {
return parent;
}
public void setParent(MainActivity parent) {
this.parent = parent;
}
}
例如,像這樣擴展片段,並在片段中使用getparent方法從mainactivity中訪問您的對象
public class BasicInformationFragment extends BaseFragment {
EditText email, pass, mobile, code;
public static final String MyPREFERENCES = "MyPrefs";
SharedPreferences sharedpreferences;
JSONObject profile;
String userUsername, password, mobileNumber, emailId;
private ProfileActivity myActivity;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getParent().yourobjectnametoaccess;
}
}
在“ ProfileActivity”類中創建Activity的靜態實例
public static ProfileActivity instance;
public static ProfileActivity getInstance() {
if (instance == null) {
instance = new ProfileActivity ();
}
return instance;
}
然后編寫具有JSON對象的方法
public JSONObject getProfile(){
return this.profile;
}
現在,在片段中的“ onCreateView”方法中,您可以按照以下方式直接訪問JSON對象“ getProfile”方法
JSONObject profileObj = ProfileActivity .getInstance().getProfile();
我希望這能幫到您。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.