Вы находитесь на странице: 1из 2

//1-Starting a new activity with an intent object

//in onCreate
Intent myIntent = new Intent();
myIntent.setAction(Intent.ACTION_CALL_BUTTON);
startActivity(myIntent);

//Replace the setAction()


myIntent.setAction(Intent.ACTION_VIEW);
myIntent.setData(android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);

//2-Switching between activities


//in onCreate->onClick
Intent intent = new Intent();
String packageName ="com.packtpub.android.activityswitcher";
String className ="com.packtpub.android.activityswitcher.MySubActivity";
intent.setClassName(packageName, className);
startActivity(intent);

//3-Returning a result from an activity


//New->onCreate
setResult(42);
finish();
//in Results->onCreate
Intent i = new Intent(this, MyNewActivity.class);
startActivityForResult(i, 0);
//out of onCreate
@Override
protected void onActivityResult(int requestCode,int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Toast.makeText(this, Integer.toString(resultCode),Toast.LENGTH_LONG).show();
}

//Returning an intent with the result code


//change New->onCreate
setResult(42, i);

//4-Storing an activity's state


//onCreate->onClick
mTextView.setText(mEditText.getText().toString);
//onSaveInstanceState
state.putString(KEY, mTextView.getText().toString());
super.onSaveInstanceState(state);
//onRestoreInstanceState
super.onRestoreInstanceState(state);
mTextView.setText(state.getString(KEY));
//Enter text/click on button/restart activity by exiting and restarting/rotating

//5-Storing persistent activity data


// onCreate
SharedPreferences settings = getPreferences(MODE_PRIVATE);
mUserName = settings.getString(KEY, "new user");
mTextView.setText("Welcome " + mUserName);
//onPuase
super.onPause();
SharedPreferences settings = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.putString(KEY, mUserName);
settings.edit().putString(KEY, mUserName).commit();
//onCreate->onClick
mUserName = mEditText.getText().toString();
mTextView.setText("Welcome " + mUserName);
//Run/Once value entered,persist across sessions/clear it using device's
//App Manager in Settings or uninstall and reinstall app
//Using more than one preference fle
getSharedPreferences(String name, int mode)

//Managing the activity lifecycle


//onCreate
mTextView.append("\n created");
//onPause
mTextView.append("\n pausing");
//Repeat this for each of the remaining lifecycle callbacks
// see if the activity is really fnishing before onDestroy() is executed in
onPause()
mTextView.append("\n pausing");
if (isFinishing()){mTextView.append(" ... finishing");}

Вам также может понравиться