11package sample .helloworld ;
22
3+ import android .app .Activity ;
34import android .app .Fragment ;
5+ import android .content .Intent ;
6+ import android .net .Uri ;
47import android .os .Bundle ;
8+ import android .text .TextUtils ;
59import android .view .LayoutInflater ;
610import android .view .View ;
711import android .view .ViewGroup ;
12+ import android .widget .TextView ;
813
14+ import com .dianping .app .MyApplication ;
915import com .dianping .loader .MyResources ;
1016
1117public class HelloFragment extends Fragment {
18+ String name ;
1219
1320 @ Override
1421 public View onCreateView (LayoutInflater inflater , ViewGroup container ,
@@ -27,4 +34,57 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
2734
2835 }
2936
37+ @ Override
38+ public void onViewCreated (View view , Bundle savedInstanceState ) {
39+ super .onViewCreated (view , savedInstanceState );
40+
41+ if (savedInstanceState != null ) {
42+ name = savedInstanceState .getString ("name" );
43+ }
44+ update ();
45+
46+ view .findViewById (R .id .start_url ).setOnClickListener (
47+ new View .OnClickListener () {
48+ @ Override
49+ public void onClick (View v ) {
50+
51+ // Start the PickerFragment by url mapping.
52+ // (app://pickname is mapped to PickerFragment, defined
53+ // in fragment.properties)
54+ Intent i = new Intent (Intent .ACTION_VIEW , Uri
55+ .parse (MyApplication .PRIMARY_SCHEME
56+ + "://pickname?selection=" + name ));
57+
58+ // We need a result, the result will be callback in
59+ // onActivityResult()
60+ startActivityForResult (i , 1 );
61+ }
62+ });
63+ }
64+
65+ @ Override
66+ public void onSaveInstanceState (Bundle outState ) {
67+ super .onSaveInstanceState (outState );
68+
69+ outState .putString ("name" , name );
70+ }
71+
72+ @ Override
73+ public void onActivityResult (int requestCode , int resultCode , Intent data ) {
74+ if (requestCode == 1 && resultCode == Activity .RESULT_OK ) {
75+ name = data == null ? null : data .getStringExtra ("selection" );
76+ update ();
77+ }
78+ }
79+
80+ private void update () {
81+ TextView tv = (TextView ) getView ().findViewById (R .id .text_hello );
82+
83+ if (TextUtils .isEmpty (name )) {
84+ tv .setText ("Hello World!" );
85+ } else {
86+ tv .setText ("Hello " + name + "!" );
87+ }
88+ }
89+
3090}
0 commit comments