问题
I'm working on an app in which has a class that extends FragmentActivity. In the onResume() method I was calling a class for which I am sending the reference of this fragmentactivity and in that class I'm replacing it with another fragment.
In that Fragment I'm dynamically adding the layout , but when I click on the button I'm Calling startActivityForResult() which in turn receives results in OnActivityResult() in which I'm trying to update the text of EditText , but it is not happening. When I clicked back button I'm able to see the text on EditText changed . Can anyone help me in sorting out this issue. I'will post my entire source code here .
MainActivity.java:
public class MainActivity extends FragmentActivity {
//FragmentActivity context;
Context context;
FragmentManager fm;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
context = MainActivity.this;
/* fm = getSupportFragmentManager();
FragmentManager fm = getSupportFragmentManager();
Fragment multiFormScreenFragment = MultiFormScreenFragment.newInstance(1 );
fm.beginTransaction()
.replace(R.id.fragment_container, multiFormScreenFragment)
.commit();
*/
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
new LoadScreenHelper((FragmentActivity)context, R.id.fragment_container).loadTargetScreen("", 1,"",1,false,"","","");
}
}
LoadScreenHelper.java:
package com.cognizant.aim.LoadScreen;
import com.android.testfragmentactivityresult.MultiFormScreenFragment;
public class LoadScreenHelper {
public FragmentActivity context;
//public Context context;
int mContainerId=-1;
public LoadScreenHelper(FragmentActivity c,int containerId){
context = c;
mContainerId = containerId ;
}
public void loadTargetScreen(String screenType,int nav_id,String usid,int screenorder, boolean isMain,String filterColumnName,String filterColumnVal,String uiHeadingTitle){//uiHeadingTitle
System.out.println("ScreenType::::"+screenType+"nav_id"+nav_id+":::"+"screenorder::::"+screenorder);
Bundle bundle = new Bundle();
bundle.putInt("screenOrder", 1);
bundle.putString("uniqueid", "1");
bundle.putString("title", "Form");
// bundle.putString("tableName", dataSourceTableName);
// bundle.putString("rowId", clicked_list_row_Id);
FragmentManager fm = context.getSupportFragmentManager();
Fragment multiFormScreenFragment = MultiFormScreenFragment.newInstance(1 /*screenId*/);
multiFormScreenFragment.setArguments(bundle);
if(isMain){
fm.beginTransaction()
.replace(mContainerId, multiFormScreenFragment)
.commit();
}
else{
fm.beginTransaction()
.replace(mContainerId, multiFormScreenFragment)
.addToBackStack("null")
.commit();
}
}
}
MultiFormScreenFragment.java:
public class MultiFormScreenFragment extends Fragment {
View view;
int mContainerId = -1;
private static final String TAG = "MultiFormScreenFragment";
LinearLayout llayout;
public static MultiFormScreenFragment newInstance(int parentid) {
MultiFormScreenFragment f = new MultiFormScreenFragment();
Bundle b = new Bundle();
b.putInt("list_id", parentid);
f.setArguments(b);
return f;
}
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
mContainerId = container.getId();
view = inflater.inflate(R.layout.activity_main, container, false);
llayout = (LinearLayout)view.findViewById(R.id.llayout);
generateUILayout();
return view;
}
private void generateUILayout() {
// TODO Auto-generated method stub
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
Button newBtn = new Button(getActivity());
newBtn.setLayoutParams(params);
newBtn.setId(1);
newBtn.setText("Demo");
llayout.addView(newBtn);
EditText newEText = new EditText(getActivity());
newBtn.setLayoutParams(params);
newEText.setId(2);
newEText.setText("Demo");
llayout.addView(newEText);
newBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(getActivity(), SecondActivity.class);
intent.putExtra("sampleData", "This is Sample Data");
startActivityForResult(intent, 1);
}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==getActivity().RESULT_OK && requestCode==1){
String msg = data.getStringExtra("returnedData");
EditText eText = (EditText)getActivity().findViewById(2);
eText.setText(msg);
}
}
}
SecondActivity.java
===================
package com.android.testfragmentactivityresult;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class SecondActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Intent intent= getIntent();
String msg = intent.getStringExtra("sampleData");
msg += ", Added at Third";
intent.putExtra("returnedData", msg);
setResult(RESULT_OK, intent);
finish();
}
}
activity_main.xml
==================
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/llayout">
</LinearLayout>
main.xml
========
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
AndroidManifest.xml
===================
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.testfragmentactivityresult"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name = ".SecondActivity"
></activity>
</application>
</manifest>
回答1:
...but when I click on the button I'm Calling startActivityForResult() which in turn receives results in OnActivityResult() in which I'm trying to update the text of EditText , but it is not happening. When I clicked back button I'm able to see the text on EditText changed
I think this is happening because of the use of LoadScreenHelper
in the onResume
method of the FragmentActivity
. Keep in mind that onResume
will always be called when the activity comes to the foregound, this will also happen after the Activity
started with startActivityForResult
will return. Now by calling LoadScreenHelper
's loadTargetScreen()
method, you'll always add a new MultiFormScreenFragment
to the initial FragmentActivity
. When you come back from the child Activity
the onResume
method will be called again and a new MultiFormScreenFragment
will be created most likely covering the initially added fragment. If you click the back button this top fragment will be removed from the screen leaving the initially added fragment.
As I don't know what you're trying to do in the end with the LoadScreenHelper
class I would suggest to either move the line:
new LoadScreenHelper((FragmentActivity)context, R.id.fragment_container).loadTargetScreen("", 1,"",1,false,"","","");
in the onCreate
method so it's executed only once, or insert a check in the loadTargetScreen
method to find out if an instance of the MultiFormScreenFragment
isn't already in the layout(use the FragmentManager
to find the fragment). If it is already in the layout then do nothing else add a new instance of the fragment.
来源:https://stackoverflow.com/questions/13939946/not-able-to-update-fragment-ui-from-onactivityresult