Saving Activity state int String Array Double on Android
private int[] mData = new int[2]; // fill some values into array!!
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean("myBoolean", true);
outState.putDouble("myDouble", 2.7);
outState.putInt("myInt", 5);
outState.putString("myString", "Heloo girls!");
int[] data = new int[mData.length];
for (int i = 0; i < data.length; i++) {
data[i] = mData[i];
}
outState.putIntArray("myArray", data);
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
boolean myBoolean = savedInstanceState.getBoolean("myBoolean", false); // false basic value
double myDouble = savedInstanceState.getDouble("myDouble", 1.5); // 1.5 basic value
int myInt = savedInstanceState.getInt("myInt", 10);
String myString = savedInstanceState.getString("myString", "Hello boys!");
int[] data = savedInstanceState.getIntArray("myArray");
if (data != null && data.length == mData.length) {
for (int i = 0; i < data.length; i++) {
mData[i] = data[i];
}
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
boolean myBoolean = savedInstanceState != null ? savedInstanceState.getBoolean("myBoolean", false) : true;
// etc. .......
}
396LW NO topic_id
AD
Další témata ....(Topics)
First cteate big icon 512x512 px formtat .PGN in graphics editor as Photoshop, Gimp, Piant.NET and save image.
If you create new project via Eclipse and choice your 512x512 image for ic_launcher.
Eclipse will make all icons from this image for new application very well.
If you create new project via Eclipse and choice your 512x512 image for ic_launcher.
Eclipse will make all icons from this image for new application very well.
Integer, Float, String, List passed as reference in JAVA example source code:
// List passed as reference JAVA
List<Integer>list = new ArrayList<Integer>();
public void fc (List<Integer>listRef){
listRef.add(7);
listRef.add(5);
}
fc(list); // 7, 5
// String passed as reference JAVA
public void mutate(AtomicReference<Object> ref) { ref.set("Goodbye"); }
AtomicReference<Object> ref = new AtomicReference<Object>("Hello");
mutate(ref);
System.out.println(ref.get()); //Goodbye!
String s = (String) ref.get();
// Integer passed as reference JAVA
private static void mutate(AtomicReference<Object> ref) { ref.set(7); }
//public static void main(String[] arg) {
AtomicReference<Object> ref = new AtomicReference<Object>(5);
mutate(ref);
System.out.println(ref.get()); // 7
int n = (Integer) ref.get();
// Float passed as reference JAVA
private static void mutate(AtomicReference<Object> ref) { ref.set(14.8f); }
//public static void main(String[] arg) {
AtomicReference<Object> ref = new AtomicReference<Object>(12.1f);
mutate(ref);
float f = (Float) ref.get();
System.out.println(f); //14.8
This version of the rendering library is more recent than your version of ADT plug-in. Please update ADT plug-in
Click on menu Help > Install New Software.
In the Work with field, Add: https://dl-ssl.google.com/android/eclipse/
Select: Developer Tools / Android Development Tools.
Click Next to complete the wizard.
If you have problem try download all sdk + eclipse in one pack , rename old folder for example Andorid_old, create new folder Android and unpack sdk + eclipse from this adress:
//developer.android.com/sdk/index.html
Click on menu Help > Install New Software.
In the Work with field, Add: https://dl-ssl.google.com/android/eclipse/
Select: Developer Tools / Android Development Tools.
Click Next to complete the wizard.
If you have problem try download all sdk + eclipse in one pack , rename old folder for example Andorid_old, create new folder Android and unpack sdk + eclipse from this adress:
//developer.android.com/sdk/index.html
Call findViewById from onCreateView
res\layout
activity_main.xml
fragment_main.xml
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.containerMoje, new PlaceholderFragment()).commit();
}
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
if (container == null) {
return null;
}
TextView mText;
mText = (TextView)rootView.findViewById(R.id.idText);
mText.setText("Hello from fragment_main");
return rootView;
}
}
}
res\layout
activity_main.xml
<FrameLayout xmlns:android="//schemas.android.com/apk/res/android"
xmlns:tools="//schemas.android.com/tools"
android:id="@+id/containerMoje"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="cz.okhelp.autoskola.MainActivity"
tools:ignore="MergeRootFrame" />
fragment_main.xml
<RelativeLayout xmlns:android="//schemas.android.com/apk/res/android"
xmlns:tools="//schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="cz.okhelp.autoskola.MainActivity$PlaceholderFragment" >
<TextView
android:id="@+id/idText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</RelativeLayout>
Show keyboard if WebView input have focus Android apps development example source code.
WebView webview;
//in onCreate
webview = (WebView) findViewById(R.id.idWebviewPda);
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl("//android.okhelp.cz");
webview.requestFocus(View.FOCUS_DOWN);
webview.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_UP:
if (!v.hasFocus()) {
v.requestFocus();
}
break;
}
return false;
}
});
Editace: 2016-03-02 08:15:58
Počet článků v kategorii: 396
Url:saving-activity-state-int-string-array-double-on-android