我有一个MainActivity,其中有些卡的名称不同。 onClick,标题通过intent作为adapter传递到secondActivity,并显示为标题。从那里,我可以去参加其他活动。如果我从这些其他活动之一中回来(通过将第二个活动建立为父活动而创建的后退按钮),标题将消失。如何保留最初作为意图传递的标头,还是应该完全不同?

我尝试在onResume()中使用onStart()secondActivity从全局变量重新分配意图。

adapter类,其中写入card onClick方法:

@Override
public void onBindViewHolder(final TripHolder tripHolder, final int position) {
    Trip trip = trips.get(position);
    tripHolder.setDetails(trip);

    tripHolder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(context, SecondActivity.class);
            TextView card_title = v.findViewById(R.id.TripNamecl);
            intent.putExtra("card_title", card_title.getText().toString());
            context.startActivity(intent);
        }
    });
}


显示标题的secondActivity

public class SecondActivity extends AppCompatActivity {
    String name;
    TextView header;
    static final String STATE_HEADER = "header";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);

        name = getIntent().getStringExtra("card_title");
        header = findViewById(R.id.TripsHeader);
        header.setText(name);
    }

    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
        super.onSaveInstanceState(savedInstanceState);
        savedInstanceState.putString(STATE_HEADER, name);
    }

    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        name = savedInstanceState.getString(STATE_HEADER);
        header.setText(name);
    }

    public void launchMapsActivity(View view) {
        Uri gmmIntentUri = Uri.parse("geo:48.8566°,2.3522");
        Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
        mapIntent.setPackage("com.google.android.apps.maps");
        if (mapIntent.resolveActivity(getPackageManager()) != null) {
            startActivity(mapIntent);
        }
    }

    public void launchTravelActivity(View view) {
        Intent intent = new Intent(this, TravelActivity.class);
        startActivity(intent);
    }

    public void launchPlansActivity(View view) {
        Intent intent = new Intent(this, PlansActivity.class);
        startActivity(intent);
    }
}


解:
解决方案是将android:launchMode =“ singleTop”放入清单文件中,以进行secondactivity。此处有更详细的描述:How can I return to a parent activity correctly?

最佳答案

为了做到这一点,您应该在SecondActivity中重写onSavedInstanceState。
您可以使用类似的方法,然后根据需要进行调整:

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);

    //here you can save your variables
    savedInstanceState.putString("myHeader", name);
}

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    //here you can retrieve your variables
    name = savedInstanceState.getString("myHeader");
}


让我知道这是否有效!祝好运

09-27 18:00