FragmentKey is an APT framework for creating fragments using newInstance to define key values

Update log:

V1.0.0 2020.1.17
  • First Release

Before use:

public TFragment newInstance(String username, String password, int age) { TFragment tFragment = new TFragment(); Bundle = new Bundle(); bundle.putString("username", username);
        bundle.putString("password", password);
        bundle.putInt("age", age);
        tFragment.setArguments(bundle);
        returntFragment; } @Override public void onAttach(Context context) { super.onAttach(context); // Use Bundle arguments = getArguments(); mUsername = arguments.getString("username");
        mPassword = arguments.getString("password");
        age = arguments.getInt("age");
    }
Copy the code

After use:

// define @inject public String mUsername; @Inject(name ="password1") public String mPassword; @Inject public int age; // TFragment2 tFragment = new TFragment2Key().send("Name"."Password", 10); @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @nullable Bundle savedInstanceState) {log. d(TAG, mUsername); Log.d(TAG, mPassword); Log.d(TAG, String.valueOf(age));return super.onCreateView(inflater, container, savedInstanceState);
    }
Copy the code

As you can see, this framework simplifies the process of passing values and avoids the hassle of using keys to pass data.

The Installation:

1.project.gradle

    buildscript {
    repositories {
        google()
        jcenter()
        maven { url 'https://jitpack.io' }
    }
    dependencies {
        classpath 'com. Android. Tools. Build: gradle: 3.2.1'}}Copy the code

2. App. Gradle added

dependencies {
   implementation 'com. Making. TanZhiL. FragmentKey: FragmentKey: 1.0.2'
    annotationProcessor 'com. Making. TanZhiL. FragmentKey: FragmentKey - compiler: 1.0.2'
}
Copy the code

Usage:

1. Add Inject annotations to fields that need to be transmitted externally, and then build

   @Inject
    public String mUsername;
	// Name is the user-defined key value. The default value is the field name
    @Inject(name = "password1")
    public String mPassword;
    @Inject
    public int age;
Copy the code

2. Use xxxkey.send (…) to create a Fragment instance. Methods; The xxxKey class is automatically generated by APT.

      TFragment2 tFragment = new TFragment2Key().send("Name"."Password".10);
Copy the code

## Note: Common types of fields that bundles can pass are currently supported

   @Inject
    protected String s;
    @Inject
    protected Integer i;
    @Inject
    protected boolean b;
    @Inject
    protected float f;
    @Inject
    protected double d;
    @Inject
    protected long l;
    @Inject
    protected ArrayList<String> ls;
    @Inject
    protected ArrayList<Integer> li;
    @Inject
    protected ArrayList<Parcelable> lp;
    @Inject
    protected Serializable se;
    @Inject
    protected Parcelable p;
Copy the code

Thank you

  • Thanks to all the open source library masters
  • Learn from the big guy github.com/JakeWharton…

The problem of feedback

Welcome to add star, call github.com/TanZhiL/Fra…

About the author

Tan Zhilong

The open source project

  • Quick Section programming open Source library github.com/TanZhiL/OkA…
  • An apt framework to solve the problem of using newInstance to create a fragment to define key values github.com/TanZhiL/Fra…
  • High imitation Himalayan listening Android client github.com/TanZhiL/Zhu…
  • Fast Persistence framework based on Object-oriented design github.com/TanZhiL/RxP…
  • Skeleton screen elastic block github.com/TanZhiL/Ske…

License

Copyright (C)  tanzhilong FragmentKey Open Source Project

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except inThe compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed toin writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Copy the code