我是靠谱客的博主 妩媚蓝天,这篇文章主要介绍Android 简单跳转页面工具的实例详解,现在分享给大家,希望可以做个参考。

事情起源

activity 或者 fragment 每次跳转传值的时候,你是不是都很厌烦那种,参数传递。
那么如果数据极其多的情况下,你的代码将苦不堪言,即使在很好的设计下,也会很蛋疼。那么今天我给大家推荐一个工具
和咱原生跳转进行比较

比较:

1.跳转方式比较

复制代码
1
2
Intenti=new Intent(this,MainActivity.class); startActivity(i);

vs

复制代码
1
ApMainActivity.getInstance().start(this);
复制代码
1
2
3
4
5
6
7
8
//发送 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","");

vs

复制代码
1
2
3
4
//发送 ApMainActivity.getInstance().setMessage("123").start(this); //接收 AutoJ.inject(this);

AutoPage

github地址 https://github.com/smartbackme/AutoPage
如果觉得不错 github 给个星
Android 容易的跳转工具

注意事项:必须有如下两个要求 androidxkotlin & java

#########使用#########
project : build.gradle 项目的gradle配置

复制代码
1
2
3
4
buildscript { repositories { maven { url 'https://dl.bintray.com/297165331/AutoPage'} }

在你的每个需要做容易跳转的模块添加如下配置
你的项目必须要支持 kapt
kotlin kapt

复制代码
1
2
3
4
apply plugin: 'kotlin-kapt' implementation 'com.kangaroo:autopage:1.0.2' kapt 'com.kangaroo:autopage-processor:1.0.2'

 

重点

@AutoPage 只能在字段或者类上标注Ap 作为前缀,为你快速跳转

kotlin:

字段必须标注 @JvmField 和 @AutoPageonCreate 中 在你的需要跳转的页面加入 AutoJ.inject(this)

java:

字段必须标注 @AutoPageonCreate 中 在你的需要跳转的页面加入 AutoJ.inject(this)

######### Activity 中使用#########

例1

简单的跳转

复制代码
1
2
3
4
5
6
7
@AutoPage class SimpleJump1Activity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_simple_jump1) } }

之后调用

复制代码
1
ApSimpleJump1Activity.getInstance().start(this)

例2

简单的跳转并且带参数

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
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 } }

之后调用

复制代码
1
ApMainActivity2.getInstance().setMessage("123").start(this)

例3:

跳转带有result

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
@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() } }

之后调用

复制代码
1
ApSimpleJumpResultActivity.getInstance().requestCode(1).start(this)

####### 在 fragment 中使用 #########

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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 } }

之后调用

复制代码
1
ApFragmentSimpleFragment.getInstance().setMessage("134").build()

到此这篇关于Android 简单跳转页面工具的文章就介绍到这了,更多相关Android跳转页面工具内容请搜索靠谱客以前的文章或继续浏览下面的相关文章希望大家以后多多支持靠谱客!

最后

以上就是妩媚蓝天最近收集整理的关于Android 简单跳转页面工具的实例详解的全部内容,更多相关Android内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(118)

评论列表共有 0 条评论

立即
投稿
返回
顶部