4

我正在开发一个应用程序,其中有一个扩展 FragmentActivity 的类。在 onResume() 方法中,我调用了一个类,我正在为其发送该片段活动的引用,并且在该类中,我将其替换为另一个片段。

在那个片段中,我正在动态添加布局,但是当我单击按钮时,我正在调用 startActivityForResult() ,它反过来在 OnActivityResult() 中接收结果,我正在尝试更新 EditText 的文本,但它是没有发生。当我单击后退按钮时,我可以看到 EditText 上的文本已更改。谁能帮我解决这个问题。我将在这里发布我的全部源代码。

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>
4

1 回答 1

2

...但是当我单击按钮时,我正在调用 startActivityForResult() ,该按钮又在 OnActivityResult() 中接收结果,在该按钮中我试图更新 EditText 的文本,但没有发生。当我单击后退按钮时,我可以看到 EditText 上的文本已更改

我认为这是因为LoadScreenHelperonResume. FragmentActivity请记住,onResume当活动到达前台时将始终被调用,这也将在Activity开始startActivityForResult返回后发生。现在通过调用LoadScreenHelper'sloadTargetScreen()方法,您将始终MultiFormScreenFragment向初始FragmentActivity. 当您从孩子那里回来时,ActivityonResume方法将被再次调用,并且MultiFormScreenFragment将创建一个新的最有可能覆盖最初添加的片段的方法。如果您单击后退按钮,该顶部片段将从屏幕中移除,留下最初添加的片段。

因为我不知道你最后想在LoadScreenHelper课堂上做什么,我建议要么移动这条线:

new LoadScreenHelper((FragmentActivity)context, R.id.fragment_container).loadTargetScreen("", 1,"",1,false,"","","");

onCreate方法中,所以它只执行一次,或者在方法中插入一个检查,以确定布局中是否还没有loadTargetScreen的实例(使用来查找片段)。如果它已经在布局中,那么什么也不做,添加片段的新实例。MultiFormScreenFragmentFragmentManager

于 2012-12-18T20:27:56.903 回答