-
Notifications
You must be signed in to change notification settings - Fork 496
/
Copy pathHelloFragment.java
90 lines (70 loc) · 2.36 KB
/
HelloFragment.java
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package sample.helloworld;
import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.dianping.app.MyApplication;
import com.dianping.loader.MyResources;
public class HelloFragment extends Fragment {
String name;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// MyResources manages the resources in specific package.
// Using a Class object to obtain an instance of MyResources.
// In this case, hello.xml is in the same package as HelloFragment class
MyResources res = MyResources.getResource(HelloFragment.class);
// Using MyResources.inflate() if you want to inflate some layout in
// this package.
return res.inflate(getActivity(), R.layout.hello, container, false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
if (savedInstanceState != null) {
name = savedInstanceState.getString("name");
}
update();
view.findViewById(R.id.start_url).setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
// Start the PickerFragment by url mapping.
// (app://pickname is mapped to PickerFragment, defined
// in fragment.properties)
Intent i = new Intent(Intent.ACTION_VIEW, Uri
.parse(MyApplication.PRIMARY_SCHEME
+ "://pickname?selection=" + name));
// We need a result, the result will be callback in
// onActivityResult()
startActivityForResult(i, 1);
}
});
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString("name", name);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1 && resultCode == Activity.RESULT_OK) {
name = data == null ? null : data.getStringExtra("selection");
update();
}
}
private void update() {
TextView tv = (TextView) getView().findViewById(R.id.text_hello);
if (TextUtils.isEmpty(name)) {
tv.setText("Hello World!");
} else {
tv.setText("Hello " + name + "!");
}
}
}