Detailed example of Android simple jump page tool

Time:2022-5-11

Origin of events

Are you tired of parameter passing every time activity or fragment jumps and passes values.
So if there is a lot of data, your code will suffer, even under good design. So today I recommend a tool for you
Compare with our native jump

Comparison:

1. Comparison of jump methods


 Intenti=new Intent(this,MainActivity.class); 
 startActivity(i);

vs


ApMainActivity.getInstance().start(this);
//Send
  Intenti=new Intent(this,MainActivity.class);
  Bundle bundle = new Bundle();
  bundle.putInt("message", "123");
  i.putExtra("Bundle", bundle);
  startActivity(i);
  //Receive
	String s=bundle.getString("message","");

vs

//Send
	ApMainActivity.getInstance().setMessage("123").start(this);
	//Receive
	AutoJ.inject(this);

AutoPage

GitHub address https://github.com/smartbackme/AutoPage
If you feel good, give GitHub a star
Android easy jump tool

Note: there must be the following two requirements: Android xkotlin & Java

#########Use#########
project : build. Gradle configuration for gradle project


buildscript {
  repositories {
    maven { url 'https://dl.bintray.com/297165331/AutoPage'}
  }

Add the following configuration to each module you need to do easy jump
Your project must support kapt
kotlin kapt


apply plugin: 'kotlin-kapt'

  implementation 'com.kangaroo:autopage:1.0.2'
  kapt 'com.kangaroo:autopage-processor:1.0.2'

 

a key

@AutoPage can only mark AP as the prefix on the field or class to jump quickly for you

kotlin:

The field must be marked with @ jvmfield and @ autopageoncreate. Add autoj to the page you need to jump to inject(this)

java:

The field must be marked with @ autopageoncreate. Add autoj to the page you need to jump to inject(this)

#########Used in activity#########

Example 1

Simple jump


@AutoPage
class SimpleJump1Activity : AppCompatActivity() {
  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_simple_jump1)
  }
}

Called after


ApSimpleJump1Activity.getInstance().start(this)

Example 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
  }
}

Called after


ApMainActivity2.getInstance().setMessage("123").start(this)

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()
  }
}

Called after


ApSimpleJumpResultActivity.getInstance().requestCode(1).start(this)

#######Use 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

  }
}

Called after


ApFragmentSimpleFragment.getInstance().setMessage("134").build()

This is the end of this article about Android simple jump page tool. For more information about Android jump page tool, please search the previous articles of developeppaer or continue to browse the relevant articles below. I hope you will support developeppaer in the future!