Problem analysis

  • StartActivity (_this,_class) and Intent putExtra() to pass parameters. Yes, it’s that simple.
  • However, using putExtra() in an Intent can be messy when the interface is hopping and has multiple parameters. More importantly, you need to be careful when retrieving data from an Intent — the type needs to be correct, the key needs to be correct, or the data fails to be retrieved or crashes the Intent. The current Activity/Fragment must know the name of the target Activity class. I wondered if this should be encapsulated, or if there was a better solution.

Scheme 1

  1. Create a single mode for unified management interface jumps in the utils package
  2. Create an intent in OpenActManager to implement the jump
  3. Pass data using serialization (Parcelable) object to achieve, create an entity class inherit Parcelable interface, will pass the field stored in the class, easy to obtain and call, no longer need to find the corresponding key to obtain parameters.
private final String OPEN_ACTIVITY_KEY = "open_activity_key"; ** @param Activity this * @param generic * @return */ public T getParcelableExtra(activity activity) { Parcelable parcelable = activity.getIntent().getParcelableExtra(OPEN_ACTIVITY_KEY); activity = null; return (T) parcelable; } /** * Start an Activity * @param _this * @param _class * @param flags * @param parcelable passed entity class */ public void goActivity(Context _this, Class _class, int flags, Parcelable parcelable) { intent.setClass(_this, _class); setFlags(flags); putParcelable(parcelable); _this.startActivity(intent); _this = null; }Copy the code

Usage:


public int id;
public String name;
public ArrayList data;Copy the code

StartActTransfer transfer = new StartActTransfer(10,    "deng", new ArrayList());
OpenActManager.get().goActivity(this, SecondActivity.class, transfer);Copy the code
StartActTransfer startActTransfer = OpenActManager.get().getParcelableExtra(this); if(startActTransfer! =null){ int id = startActTransfer.id; String name = startActTransfer.name; ArrayList data = startActTransfer.data; LogHelper.e("id:"+id+" "+"name:"+name); }Copy the code

Complete example code github

Plan (2)

  • Use the Android routing table framework
  • www.sixwolf.net/blog/2016/0…

We are standing on the shoulders of giants reference: www.jianshu.com/p/0590f530c… www.sixwolf.net/blog/2016/0…