Android development is still in use
Intenti=new Intent(this,MainActivity.class);
Bundle bundle = new Bundle();
bundle.putInt("message", "123");
i.putExtra("Bundle", bundle);
startActivity(i);
Copy the code
Send data? Still use
String s=bundle.getString("message","");
Copy the code
Receive data? If you don’t want to define the tag name and reduce the constant usage, try this tool
Origin of things
Do you get tired of passing parameters every time your activity or fragment jumps to a value? So if you have a lot of data, your code will suffer, and even with good design, it will suffer. So today I would like to recommend a tool to compare with our original jump
Comparison:
1. Comparison of jump modes
Intenti=new Intent(this,MainActivity.class);
startActivity(i);
Copy the code
vs
ApMainActivity.getInstance().start(this);
Copy the code
Intenti=new Intent(this, mainactivity.class); Bundle bundle = new Bundle(); bundle.putInt("message"."123");
i.putExtra("Bundle", bundle); startActivity(i); String s=bundle.getString()"message"."");
Copy the code
vs
/ / send ApMainActivity. GetInstance (). SetMessage ("123").start(this); / / receive AutoJ. Inject (this);Copy the code
AutoPage
Making address github.com/smartbackme… Github gives Android an easy jump tool if you like
Note: The following two requirements must be met
- androidx
- kotlin & java
# # # # # # # # # # # # # # # # # # project: build. Gradle gradle configuration of the project
buildscript {
repositories {
maven { url 'https://dl.bintray.com/297165331/AutoPage'}}Copy the code
Add the following configuration to each module you need to make easy jumps. Your project must support Kapt kotlin kapt
Apply the plugin: 'kotlin - kapt' implementation 'com. The kangaroo: autopage: 1.0.2 kapt' com. The kangaroo: autopage - processor: 1.0.2 'Copy the code
Focus on
- @autopage can only be annotated on fields or classes
- Ap as a prefix, for you to jump quickly
Kotlin:
- Fields must be annotated @jVMField and @autopage
- Add autoj.inject (this) to the page you want to jump to in onCreate
Java:
- Fields must be marked @autopage
- Add autoj.inject (this) to the page you want to jump to in onCreate
Use ######### in ######### Activity
Case 1
Simple jump
@AutoPage
class SimpleJump1Activity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_simple_jump1)
}
}
Copy the code
After the call
ApSimpleJump1Activity.getInstance().start(this)
Copy the code
Case 2
Simple jump with parameters
class MainActivity2 : AppCompatActivity() {
@AutoPage
@JvmField
var message:String? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main2)
AutoJ.inject(this)
findViewById<TextView>(R.id.text).text = message
}
}
Copy the code
After the call
ApMainActivity2.getInstance().setMessage("123").start(this)
Copy the code
Example 3:
Jump with result
@AutoPage class SimpleJumpResultActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_simple_jump_result) } override fun onBackPressed() { var intent = Intent() intent.putExtra("message","123") setResult(RESULT_OK,intent) super.onBackPressed() } }Copy the code
After the call
ApSimpleJumpResultActivity.getInstance().requestCode(1).start(this)
Copy the code
####### uses ######### in fragment
class FragmentSimpleFragment : Fragment() { @AutoPage @JvmField var message:String? = null companion object { fun newInstance() = FragmentSimpleFragment() } private lateinit var viewModel: SimpleViewModel override fun onCreateView( inflater: LayoutInflater, container: ViewGroup? , savedInstanceState: Bundle? ) : View { return inflater.inflate(R.layout.simple_fragment, container, false) } override fun onActivityCreated(savedInstanceState: Bundle?) { super.onActivityCreated(savedInstanceState) AutoJ.inject(this) viewModel = ViewModelProvider(this).get(SimpleViewModel::class.java) view? .findViewById<TextView>(R.id.message)? .text = message } }Copy the code
After the call
ApFragmentSimpleFragment.getInstance().setMessage("134").build()
Copy the code