在 return 期间将结果保存到主要 activity 期间收集多个额外的意图

Gathering multiple intent extras while saving the results during return to the main activity

当我使用另一个意图打开 activity 和 return 结果时,我在存储之前 activity 的意图额外内容时遇到问题。

逻辑:

问题是我不断丢失从 a 到 b 的 intentExtra。

如果我的描述不全面,我深表歉意,我是 Java 的新手。

Activity一个

public class PipesInspection extends AppCompatActivity{
private static final String TAG = "PipesInspection";
ArrayList<String> pipesInspectionSelection = new ArrayList<String>();
Button nextButtonToPost;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.residential_pipes_inspection_lv_selected);
    Log.d(TAG, "onCreate: starting");
    initialiseWidgets();
}
public void initialiseWidgets(){
    nextButtonToPost = (Button) findViewById(R.id.nextButton);
    nextButtonToPost.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String final_category_selection = "";
            for (String Selections : pipesInspectionSelection){
                final_category_selection = final_category_selection + Selections + ", ";
            }
            Log.v(TAG,"gotten text: " + final_category_selection);
            String selectedChoices = pipesInspectionSelected.getText().toString();
            Intent pipesInspectionIntent = new Intent(v.getContext(), PostAJobActivity.class);
            pipesInspectionIntent.putExtra("selectedChoices", selectedChoices);
            v.getContext().startActivity(pipesInspectionIntent);
        }
    });
}
}

Activity B

public class PostAJobActivity extends AppCompatActivity {
private static final String TAG = "PostAJobActivity";
EditText jobTitle, jobDescription, jobLocation;
String location, title;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_post_a_job);
    Log.d(TAG, "onCreate: starting");
    jobDescription = (EditText)findViewById(R.id.input_job_description);
    mapsButton = (ImageButton) findViewById(R.id.mapButton);
    mapsButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(mContext, LaunchMapsActivity.class);
            startActivity(intent);
        }
    });
        getIntentExtras();
}
 public void getIntentExtras(){
    jobLocation = (EditText) findViewById(R.id.location);
    Intent intentLocation =getIntent();
    location= intentLocation.getStringExtra("location");
    jobLocation.setText(location);


    jobTitle = (EditText) findViewById(R.id.title);
    Intent pipesInspectionIntent = getIntent();
    title = pipesInspectionIntent.getStringExtra("selectedChoices");
    jobTitle.setText(title);
}
}

Activity C

public class PlaceListAdapter extends RecyclerView.Adapter<PlaceListAdapter.PlaceViewHolder> {

private Context mContext;
private PlaceBuffer mPlaces;


public PlaceListAdapter(Context context, PlaceBuffer places) {
    this.mContext = context;
    this.mPlaces = places;
}

@Override
public PlaceViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    // Get the RecyclerView item layout
    LayoutInflater inflater = LayoutInflater.from(mContext);
    View view = inflater.inflate(R.layout.maps_item_place_card, parent, false);

    return new PlaceViewHolder(view);
}

@Override
public void onBindViewHolder(final PlaceViewHolder holder, int position) {
    String placeName = mPlaces.get(position).getName().toString();
    String placeAddress = mPlaces.get(position).getAddress().toString();
    holder.nameTextView.setText(placeName);
    holder.addressTextView.setText(placeAddress);
    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intentLocation = new Intent(v.getContext(), PostAJobActivity.class);
            intentLocation.putExtra("location",holder.nameTextView.getText().toString()+
                    ", " + holder.addressTextView.getText().toString());
                    v.getContext().startActivity(intentLocation);
        }
        });

}

我认为这篇文章可能会有帮助 https://developer.android.com/training/basics/intents/result.html

我不确定我是否正确理解了问题...activity A 首先启动并将数据传递给 activity B,后者调用 activity C 并从中获取数据 return 到 activity B,它使用从两个活动收集的数据来做某事 ....如果这是你的问题,你不应该开始 activity C 你应该使用 startActivityforresult() 形式的正常方式 activity B 来开始活动 C 然后等待它完成然后 return 到 B 与从 C 收集的数据....从 activity C 开始 activity B 将创建 B 的新实例而不收集数据在使用这种方式形成 A 时,带有来自 A 的数据的 B 实例将等待 C 完成并从中获取数据,此外还会在 onClick()

中移动 getIntentExtras() 函数