Okhelp.cz

Recepty, články, nápady, programování. Dříve dum-zahrada, finance, internet a know-how.okhelp.cz Pro lepší výsledky hledání používejte i diakritiku.

onSaveInstanceState() onRestoreInstanceState() basic Android example


public class MyClass extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       }
	 String sMyText = "some text";
	 int nMyInt = 10;

 @Override
 protected void onSaveInstanceState(Bundle outState) {
     // Save away the original text, so we still have it if the activity
     // needs to be killed while paused.
     outState.putString("my_text", sMyText);
     outState.putInt("my_int", nMyInt);
     Toast.makeText(this, "onSaveInstanceState()", Toast.LENGTH_LONG).show();
Log.i("onSaveInstanceState", "onSaveInstanceState()");
}
 String sNewMyText = "";
 int nNewMyInt = 0;
 
 @Override
 protected void onRestoreInstanceState(Bundle savedInstanceState) {
     super.onRestoreInstanceState(savedInstanceState);
     // restore saved values
     sNewMyText = savedInstanceState.getString("my_text");
     nNewMyInt = savedInstanceState.getInt("my_int");
     Toast.makeText(this, "onRestoreInstanceState()", Toast.LENGTH_LONG).show();
     Log.i("onRestoreInstanceState", "onRestoreInstanceState()");
     
 }
}

396LW NO topic_id




AD

Další témata ....(Topics)


141

Create monochromatic bitmap Android example | create-monochromatic-bitmap-android-example



public class MainActivity extends Activity {
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(new SampleView(this));
	}

	private static class SampleView extends View {

		// CONSTRUCTOR
		public SampleView(Context context) {
			super(context);
			setFocusable(true);

		}
		@Override
		protected void onDraw(Canvas canvas) {
			Paint paint = new Paint();
			
			canvas.drawColor(Color.GREEN);
            
            Bitmap b = Bitmap.createBitmap(200, 200, Bitmap.Config.ALPHA_8);
             paint.setColor(Color.BLUE);
            
           Canvas c = new Canvas(b);
           c.drawRect(0, 0, 200, 200, paint);
           
           canvas.drawBitmap(b, 10,10, paint);
		}

	}
}



android/monochromatic-bitmap.png
222

Disable enable internet connection in Android Emulator | disable-enable-internet-connection-in-android-emulator


If you try function for checking internet connection you can disable internet on the emulator:
Settings - Wireless and networks - Mobile networks - Data enabled (checked - unchecked )


 public boolean isNetworkAvailable() {
        ConnectivityManager cm = (ConnectivityManager) 
          getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = cm.getActiveNetworkInfo();
        if (networkInfo != null && networkInfo.isConnected()) {
            return true;
        }
        return false;
    }