生活随笔
收集整理的這篇文章主要介紹了
Android---如何返回上一Activity
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
Android 代碼用來返回上一個(gè)activity 調(diào)用onKeyDown()時(shí)發(fā)生java.lang.NullPointerException.
來自:CSDN博客推薦文章 ??|??時(shí)間:2012-05-04 22:42:12
原文鏈接:?http://blog.csdn.net/weiyirong/article/details/7536495
很多人想從一個(gè)Activity返回到上一級(jí)Activity時(shí),考慮通過在點(diǎn)擊事件里調(diào)用OnKeyDown事件來返回上一級(jí),如下:
toolbarBack.setOnClickListener(newOnClickListener(){@Overridepublicvoid onClick(View v){OnKeyDown(KeyEvent.KEYCODE_BACK,null);}}); 會(huì)發(fā)現(xiàn)報(bào)空指針錯(cuò)誤,具體原因看下源碼,你沒傳入的參數(shù)(即null)在OnKeyDown方法中使用到了,所以空指針。
其實(shí)返回上一級(jí),不用考慮如此復(fù)雜,我們可以使用如下:直接結(jié)束當(dāng)前Activity不就行了。測(cè)試可用
toolbarBack.setOnClickListener(newOnClickListener()
{@Overridepublicvoid onClick(View v){
ReaderActivity.this.finish();
}
}); 注意:
要在第一個(gè)activity向第二個(gè)activity跳轉(zhuǎn)的時(shí)候?qū)⒌谝粋€(gè)activity finish(),這樣在第二個(gè)activity返回第一個(gè)activity時(shí),就會(huì)執(zhí)行第一個(gè)activity的onCreate()方法。 ? 我嘗試著在第一種方法中使用如下: toolbarBack.setOnClickListener(newOnClickListener(){@Overridepublicvoid onClick(View v){OnKeyDown(KeyEvent.KEYCODE_BACK,newKeyEvent(KeyEvent.KEYCODE_BACK,KeyEvent.ACTION_DOWN));}});
雖說無空指針錯(cuò)誤,但是沒任何效果。我也不解,望知情的兄弟告知一聲哈!
下面我再貼一些其他的 方法
Android實(shí)戰(zhàn)總結(jié)之返回鍵返回上一級(jí)Activity(Intent的一種用法)
實(shí)現(xiàn)功能: 有兩個(gè)Activity,一個(gè)為tabActivity,一個(gè)為EditActivity,tabActivity進(jìn)入EditActivity后,在EditActivity中單擊返回鍵返回tabActivity.
其實(shí)很簡(jiǎn)單,這其中涉及到onKeyDown(),和Intent。
只要在EditActivity中重寫onKeyDown()實(shí)現(xiàn)捕獲返回鍵,再加一Intent實(shí)現(xiàn)Activity的跳轉(zhuǎn)。
具體實(shí)現(xiàn):
?
| 1 2 3 4 5 6 7 8 9 10 11 | @Override ????public boolean onKeyDown(int keyCode, KeyEvent event) ????{ ????????if(keyCode == KeyEvent.KEYCODE_BACK){ ????????????Intent myIntent = new Intent(); ????????????myIntent = new Intent(EditActivity.this, tabActivity.class); ????????????startActivity(myIntent); ????????????this.finish(); ????????} ????????return super.onKeyDown(keyCode, event); ????} |
3.在A中用startActivityForResult(Intent intent,int requestcode)啟動(dòng)B,值得注意的是requestcode里的值在多個(gè)Activity時(shí)使用Reuest_OK,無法從B返回到a的onActivityResult。
重寫A的onActivityResult(int requestCode, int resultCode, Intent data),B在返回前使用setResult(int resultCode,Intent data)設(shè)置返回的數(shù)據(jù),最后調(diào)用b的finish后會(huì)返回A的onActivityResult方法。
看例子
3.11 返回?cái)?shù)據(jù)到前一個(gè)Activity
startActivityForResult方法
范例說明
上一個(gè)范例中,好不容易將數(shù)據(jù)從Activity1傳遞至Activity2,如果要再回到Activity1,數(shù)據(jù)該不會(huì)要再封裝一次吧?而且前一個(gè)Activity1早就被程序destroy了,倘若在Activity1最后以finish() 結(jié)束程序,再通過Activity2將數(shù)據(jù)采用Bundle的方式通過新打開Activity1傳遞參數(shù),這樣的做法雖然也可以恢復(fù)User輸入的數(shù)據(jù),但是并不符合我們的期待,尤其是User曾經(jīng)輸入過的數(shù)據(jù),如果不小心點(diǎn)擊回到上一頁,數(shù)據(jù)就消失不見,這就不妙了。
有鑒于科技始終來自于人性,如果要在次頁面加上一個(gè)"回上頁"的按鈕,而非通過模擬器的回復(fù)鍵,且回上頁后又能保留之前輸入的相關(guān)信息,那么就必須使用startActivityForResult()來喚起一個(gè)Activity。利用這個(gè)方法,前一個(gè)Activity1便會(huì)有一個(gè)等待次Activity2的返回,而返回的數(shù)據(jù)就可以達(dá)到我們想要的結(jié)果。
運(yùn)行結(jié)果
| ?? ?? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ?? ?? ?? ? ?? ? ? |
| (點(diǎn)擊查看大圖)圖3-11? 將數(shù)據(jù)返回到前一個(gè)Activity |
范例程序
src/irdc.ex03_11/EX03_11.java? 在Activity1主程序中調(diào)用Activity的方法更改成startActivityForResult(intent,0),其中0為下一個(gè)Activity要返回值的依據(jù),可指定為自行定義的參考標(biāo)識(shí)符(Identifier)。程序覆蓋了onActivityResult() 這個(gè)方法,令程序在收到result后,再重新加載寫回原本輸入的值。
package?irdc.ex03_11; ??/*?import相關(guān)class?*/?import?android.app.Activity; ?import?android.content.Intent; ?import?android.os.Bundle; ?import?android.view.View; ?import?android.widget.Button; ?import?android.widget.EditText; ? 10. import?android.widget.RadioButton; ?
11. ? 12. public?class?EX03_11?extends?Activity? ?
13. { ?
14. ??private?EditText?et; ?15. ??private?RadioButton?rb1; ?16. ??private?RadioButton?rb2; ?17. ???? ?18. ??/**?Called?when?the?activity?is?first?created.?*/?19. ??@Override?20. ??public?void?onCreate(Bundle?savedInstanceState)? ?21. ??{ ?22. ????super.onCreate(savedInstanceState); ?23. ????/*?載入main.xml?Layout?*/?24. ????setContentView(R.layout.main); ?25. ???? ?26. ????/*?以findViewById()取得Button對(duì)象,并添加onClickListener?*/?27. ????Button?b1?=?(Button)?findViewById(R.id.button1); ?28. ????b1.setOnClickListener(new?Button.OnClickListener() ?29. ????{ ?30. ??????public?void?onClick(View?v) ?31. ??????{ ?32. ????????/*取得輸入的身高*/?33. ????????et?=?(EditText)?findViewById(R.id.height); ?34. ????????double?height=Double.parseDouble(et.getText().toString()); ?35. ????????/*取得選擇的性別*/?36. ????????String?sex=""; ?37. ????????rb1?=?(RadioButton)?findViewById(R.id.sex1); ?38. ????????rb2?=?(RadioButton)?findViewById(R.id.sex2); ?39. ????????if(rb1.isChecked()) ?40. ????????{ ?41. ??????????sex="M"; ?42. ????????} ?43. ????????else?44. ????????{ ?45. ??????????sex="F"; ?46. ????????}???? ?47. ???????? ?48. ????????/*new一個(gè)Intent對(duì)象,并指定class*/?49. ????????Intent?intent?=?new?Intent(); ?50. ????????intent.setClass(EX03_11.this,EX03_11_1.class); ?51. ???????? ?52. ????????/*new一個(gè)Bundle對(duì)象,并將要傳遞的數(shù)據(jù)傳入*/?53. ????????Bundle?bundle?=?new?Bundle(); ?54. ????????bundle.putDouble("height",height); ?55. ????????bundle.putString("sex",sex); ?56. ?????? ?57. ????????/*將Bundle對(duì)象assign給Intent*/?58. ????????intent.putExtras(bundle); ?59. ?????? ?60. ????????/*調(diào)用Activity?EX03_11_1*/?61. ????????startActivityForResult(intent,0); ?62. ??????} ?63. ????}); ?64. ??} ?65. ?? ?66. ??/*?覆蓋?onActivityResult()*/?67. ??@Override?68. ??protected?void?onActivityResult(int?requestCode,?int?resultCode, ?69. ??????????????????????????????????Intent?data) ?70. ??{ ?71. ????switch?(resultCode) ?72. ????{? ?73. ??????case?RESULT_OK: ?74. ????????/*?取得來自Activity2的數(shù)據(jù),并顯示于畫面上?*/?? ?75. ????????Bundle?bunde?=?data.getExtras(); ?76. ????????String?sex?=?bunde.getString("sex"); ?77. ????????double?height?=?bunde.getDouble("height"); ?78. ???????? ?79. ????????et.setText(""+height); ?80. ????????if(sex.equals("M")) ?81. ????????{ ?82. ??????????rb1.setChecked(true); ?83. ????????} ?84. ????????else?85. ????????{ ?86. ??????????rb2.setChecked(true); ?87. ????????} ?88. ????????break; ?89. ??????default: ?90. ????????break; ?91. ?????}? ?92. ???}? ? 93. }?
src/irdc.ex03_11/EX03_11_1.java
在Activity2的主程序中,設(shè)計(jì)當(dāng)Button被點(diǎn)擊時(shí),將Bundle對(duì)象與結(jié)果返回給前一個(gè)Activity1。
package?irdc.ex03_11; ??/*?import相關(guān)class?*/?import?java.text.DecimalFormat; ?import?java.text.NumberFormat; ?import?android.app.Activity; ?import?android.content.Intent; ?import?android.os.Bundle; ?import?android.view.View; ? 10. import?android.widget.Button; ?
11. import?android.widget.TextView; ?
12. ? 13. public?class?EX03_11_1?extends?Activity? ?
14. { ?
15. ??Bundle?bunde; ?16. ??Intent?intent; ?17. ??/**?Called?when?the?activity?is?first?created.?*/?18. ??@Override?19. ??public?void?onCreate(Bundle?savedInstanceState)? ?20. ??{ ?21. ????super.onCreate(savedInstanceState); ?22. ????/*?載入mylayout.xml?Layout?*/?23. ????setContentView(R.layout.myalyout); ?24. ???? ?25. ????/*?取得Intent中的Bundle對(duì)象?*/?26. ????intent=this.getIntent(); ?27. ????bunde?=?intent.getExtras(); ?28. ???? ?29. ????/*?取得Bundle對(duì)象中的數(shù)據(jù)?*/?30. ????String?sex?=?bunde.getString("sex"); ?31. ????double?height?=?bunde.getDouble("height"); ?32. ???? ?33. ????/*?判斷性別?*/?34. ????String?sexText=""; ?35. ????if(sex.equals("M")) ?36. ????{ ?37. ??????sexText="男性"; ?38. ????} ?39. ????else?40. ????{ ?41. ??????sexText="女性"; ?42. ????} ?43. ???? ?44. ????/*?取得標(biāo)準(zhǔn)體重?*/?45. ????String?weight=this.getWeight(sex,?height); ?46. ???? ?47. ????/*?設(shè)置輸出文字?*/?48. ????TextView?tv1=(TextView)?findViewById(R.id.text1); ?49. ????tv1.setText("你是一位"+sexText+"\n你的身高是"+height+ ?50. ???????????????????"厘米\n你的標(biāo)準(zhǔn)體重是"+weight+"公斤"); ?51. ???? ?52. ????/*?以findViewById()取得Button對(duì)象,并添加onClickListener?*/?53. ????Button?b1?=?(Button)?findViewById(R.id.button1); ?54. ????b1.setOnClickListener(new?Button.OnClickListener() ?55. ????{ ?56. ??????public?void?onClick(View?v) ?57. ??????{?????????? ?58. ????????/*?返回result回上一個(gè)activity?*/?59. ????????EX03_11_1.this.setResult(RESULT_OK,?intent); ?60. ???????? ?61. ????????/*?結(jié)束這個(gè)activity?*/?62. ????????EX03_11_1.this.finish(); ?63. ??????} ?64. ????}); ?65. ??} ?66. ?? ?67. ??/*?四舍五入的method?*/?68. ??private?String?format(double?num) ?69. ??{ ?70. ????NumberFormat?formatter?=?new?DecimalFormat("0.00"); ?71. ????String?s=formatter.format(num); ?72. ????return?s; ?73. ??} ?74. ?75. ??/*?以findViewById()取得Button對(duì)象,并添加onClickListener?*/?? ?76. ??private?String?getWeight(String?sex,double?height) ?77. ??{ ?78. ????String?weight=""; ?79. ????if(sex.equals("M")) ?80. ????{ ?81. ??????weight=format((height-80)*0.7); ?82. ????} ?83. ????else?84. ????{ ?85. ??????weight=format((height-70)*0.6); ?86. ????}?? ?87. ????return?weight; ?88. ??} ? 89. }?
res/layout/mylayout.xml
mylayout.xml為Activity2(EX03_11_1)的Layout,其中定義了顯示計(jì)算結(jié)果的TextView與回上一頁的Button按鈕。
<?xml?version="1.0"?encoding="utf-8"?> ?<AbsoluteLayout ???android:layout_width="fill_parent"???android:layout_height="fill_parent"???xmlns:android="http://schemas.android.com/apk/res/android"?> ???<TextView ?????android:id="@+id/text1"?????android:layout_width="wrap_content"?10. ????android:layout_height="wrap_content"?11. ????android:textSize="20sp"?12. ????android:layout_x="50px"?13. ????android:layout_y="72px"?14. ??> ?15. ??</TextView> ?16. ??<Button ?17. ????android:id="@+id/button1"?18. ????android:layout_width="100px"?19. ????android:layout_height="48px"?20. ????android:text="回上一頁"?21. ????android:layout_x="110px"?22. ????android:layout_y="180px"?23. ??> ?24. ??</Button> ? 25. </AbsoluteLayout>?
AndroidManifest.xml
范例中有兩個(gè)Activity,所以AndroidManifest.xml里必須有這兩個(gè)activity的聲明,否則系統(tǒng)將無法運(yùn)行。
<?xml?version="1.0"?encoding="utf-8"?> ?<manifest ???xmlns:android="http://schemas.android.com/apk/res/android"???package="irdc.ex03_11"???android:versionCode="1"???android:versionName="1.0.0"> ???<application ?????android:icon="@drawable/icon"? ?????android:label="@string/app_name"> ?10. ????<activity ?11. ??????android:name=".EX03_11"?12. ??????android:label="@string/app_name"> ?13. ??????<intent-filter> ?14. ????????<action?android:name="android.intent.action.MAIN"?/> ?15. ????????<category?android:name="android.intent.category.LAUNCHER"?/> ?16. ??????</intent-filter> ?17. ????</activity> ?18. ????<activity?android:name="EX03_11_1"></activity> ?19. ??</application> ? 20. </manifest>?
擴(kuò)展學(xué)習(xí)
范例中為了在回到上一頁時(shí),能夠顯示之前所輸入的數(shù)據(jù),故將原本傳遞次Activity的Intent(里面包含了有數(shù)據(jù)的Bundle對(duì)象)再重新返回給主Activity1。如果要在次Activity2中返回其它的數(shù)據(jù),例如,經(jīng)過計(jì)算后的結(jié)果、數(shù)據(jù),此時(shí)只需將要返回的數(shù)據(jù)再放入Bundle對(duì)象中即可達(dá)成。
此外,以本范例而言,其實(shí)使用startActivity()也可達(dá)成同樣的結(jié)果,僅需在主Activity被create時(shí)去判斷Intent內(nèi)有沒有數(shù)據(jù),有的話,就將數(shù)據(jù)帶入;沒有的話,就帶入空值(null)。但程序還需要再做有無值的比較,較為繁瑣,既然Android API中有提供更好用的方法,何來不用的道理?更何況如果系統(tǒng)不是只有幾行代碼,而是幾十行、幾百行代碼,那時(shí)頭可就大了!
?
轉(zhuǎn)載于:https://www.cnblogs.com/zhengbeibei/archive/2013/01/17/2865235.html
總結(jié)
以上是生活随笔為你收集整理的Android---如何返回上一Activity的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。