Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Intent intent = new Intent(getBaseContext(), SignoutActivity.class);
- intent.putExtra("EXTRA_SESSION_ID", sessionId);
- startActivity(intent);
- String s = getIntent().getStringExtra("EXTRA_SESSION_ID");
- Intent i = new Intent(getApplicationContext(), NewActivity.class);
- i.putExtra("key","value");
- startActivity(i);
- Bundle extras = getIntent().getExtras();
- if (extras != null) {
- String value = extras.getString("key");
- //The key argument here must match that used in the other activity
- }
- Intent myIntent = new Intent(this, NewActivity.class);
- myIntent.putExtra("firstName", "Your First Name Here");
- myIntent.putExtra("lastName", "Your Last Name Here");
- startActivity(myIntent)
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.view);
- Intent intent = getIntent();
- String fName = intent.getStringExtra("firstName");
- String lName = intent.getStringExtra("lastName");
- }
- Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
- intent.putExtra("Variable name", "Value you want to pass");
- startActivity(intent);
- long value = getIntent().getLongExtra("Variable name which you sent as an extra", defaultValue(you can give it anything));
- String value = getIntent().getStringExtra("Variable name which you sent as an extra");
- Boolean value = getIntent().getBooleanExtra("Variable name which you sent as an extra", defaultValue);
- import android.content.Intent;
- public class IntentHelper {
- public static final Intent createYourSpecialIntent(Intent src) {
- return new Intent("YourSpecialIntent").addCategory("YourSpecialCategory").putExtras(src);
- }
- }
- IntentHelper.createYourSpecialIntent(getIntent());
- IntentHelper.createYourSpecialIntent(getIntent()).putExtra("YOUR_FIELD_NAME", fieldValueToSave);
- getIntent().getStringExtra("YOUR_FIELD_NAME");
- public class MyActivity extends Activity {
- public static String SharedString;
- public static SomeObject SharedObject;
- //...
- Intent intent = new Intent(getBaseContext(), NextActivity.class);
- Foo foo = new Foo();
- intent.putExtra("foo", foo);
- startActivity(intent);
- Foo foo = getIntent().getExtras().getParcelable("foo");
- Intent i = new Intent(this, ActivityTwo.class);
- AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete);
- String getrec=textView.getText().toString();
- Bundle bundle = new Bundle();
- bundle.putString(“stuff”, getrec);
- i.putExtras(bundle);
- startActivity(i);
- Bundle bundle = getIntent().getExtras();
- String stuff = bundle.getString(“stuff”);
- String str = "My Data"; //Data you want to send
- Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
- intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- intent.putExtra("name",str); //Here you will add the data into intent to pass bw activites
- v.getContext().startActivity(intent);
- import android.content.Intent;
- String name = this.getIntent().getStringExtra("name");
- SharedPreferences preferences = getSharedPreferences("session",getApplicationContext().MODE_PRIVATE);
- Editor editor = preferences.edit();
- editor.putString("sessionId", sessionId);
- editor.commit();
- SharedPreferences preferences = getSharedPreferences("session", getApplicationContext().MODE_PRIVATE);
- String sessionId = preferences.getString("sessionId", null);
- SharedPreferences settings = context.getSharedPreferences("session", Context.MODE_PRIVATE);
- settings.edit().clear().commit();
- int n= 10;
- Intent in = new Intent(From_Activity.this,To_Activity.class);
- Bundle b1 = new Bundle();
- b1.putInt("integerNumber",n);
- in.putExtras(b1);
- startActivity(in);
- Bundle b2 = getIntent().getExtras();
- int m = 0;
- if(b2 != null)
- {
- m = b2.getInt("integerNumber");
- }
- i = new Intent(FirstActivity.this,SecondActivity.class);
- i.putExtra("key", value);
- startActivity(i)
- Bundle bundle= getIntent().getExtras();
- Intent intent = new Intent(this, Activity.class);
- intent.putExtra("bitmap", bitmap);
- Bitmap bitmap = getIntent().getParcelableExtra("bitmap");
- SecondFragment fragment = new SecondFragment();
- Bundle bundle = new Bundle();
- bundle.putParcelable("bitmap", bitmap);
- fragment.setArguments(bundle);
- Bitmap bitmap = getArguments().getParcelable("bitmap");
- Intent intent = new Intent(this, SecondActivity.class);
- ByteArrayOutputStream stream = new ByteArrayOutputStream();
- bitmap.compress(Bitmap.CompressFormat.JPG, 100, stream);
- byte[] bytes = stream.toByteArray();
- intent.putExtra("bitmapbytes",bytes);
- byte[] bytes = getIntent().getByteArrayExtra("bitmapbytes");
- Bitmap bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
- Intent intent = new Intent(YourCurrentActivity.this, YourActivityName.class);
- intent.putExtra("NAme","John");
- intent.putExtra("Id",1);
- startActivity(intent);
- int id = getIntent.getIntExtra("id", /* defaltvalue */ 2);
- Intent i = getIntent();
- String name = i.getStringExtra("name");
- Intent intent = new Intent(currentActivity.this, TargetActivity.class);
- intent.putExtra("booktype", "favourate");
- startActivity(intent);
- Bundle b = getIntent().getExtras();
- String typesofbook = b.getString("booktype");
- SharedPreferences pref = myContexy.getSharedPreferences("Session
- Data",MODE_PRIVATE);
- SharedPreferences.Editor edit = pref.edit();
- edit.putInt("Session ID", session_id);
- edit.commit();
- SharedPreferences pref = myContexy.getSharedPreferences("Session Data", MODE_PRIVATE);
- session_id = pref.getInt("Session ID", 0);
- /*
- * If you are from transferring data from one class that doesn't
- * extend Activity, then you need to do something like this.
- */
- public class abc {
- Context context;
- public abc(Context context) {
- this.context = context;
- }
- public void something() {
- context.startactivity(new Intent(context, anyone.class).putextra("key", value));
- }
- }
- public class Info
- {
- public static int ID = 0;
- public static String NAME = "TEST";
- }
- Info.ID
- Info.NAME
- Info.ID = 5;
- Info.NAME = "USER!";
- ...
- <application android:name="MyApplication"
- android:allowBackup="true"
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name"
- android:theme="@style/AppTheme" >
- ....
- public class MyApplication extends Application {
- private MainActivity mainActivity;
- @Override
- public void onCreate() {
- super.onCreate();
- }
- public void setMainActivity(MainActivity activity) { this.mainActivity=activity; }
- public MainActivity getMainActivity() { return mainActivity; }
- }
- public class MainActivity extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- ((MyApplication)getApplication()).setMainActivity(this);
- }
- ...
- }
- public class MyPreferences extends PreferenceActivity
- implements SharedPreferences.OnSharedPreferenceChangeListener {
- @SuppressWarnings("deprecation")
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- addPreferencesFromResource(R.xml.preferences);
- PreferenceManager.getDefaultSharedPreferences(this)
- .registerOnSharedPreferenceChangeListener(this);
- }
- @Override
- public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
- if (!key.equals("autostart")) {
- ((MyApplication)getApplication()).getMainActivity().refreshUI();
- }
- }
- }
- public class HomeIntent extends Intent {
- private static final String ACTION_LOGIN = "action_login";
- private static final String ACTION_LOGOUT = "action_logout";
- private static final String ARG_USERNAME = "arg_username";
- private static final String ARG_PASSWORD = "arg_password";
- public HomeIntent(Context ctx, boolean isLogIn) {
- this(ctx);
- //set action type
- setAction(isLogIn ? ACTION_LOGIN : ACTION_LOGOUT);
- }
- public HomeIntent(Context ctx) {
- super(ctx, HomeActivity.class);
- }
- //This will be needed for receiving data
- public HomeIntent(Intent intent) {
- super(intent);
- }
- public void setUserName(String userName, String password) {
- putExtra(ARG_USERNAME, userName);
- putExtra(ARG_PASSWORD, password);
- }
- public String getUsername() {
- return getStringExtra(ARG_USERNAME);
- }
- public String getPassword() {
- return getStringExtra(ARG_PASSWORD);
- }
- //To separate the params is for which action, we should create action
- public boolean isActionLogIn() {
- return getAction().equals(ACTION_LOGIN);
- }
- public boolean isActionLogOut() {
- return getAction().equals(ACTION_LOGOUT);
- }
- }
- public class SplashActivity extends AppCompatActivity {
- @Override
- protected void onCreate(@Nullable Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_splash);
- String username = "phearum";
- String password = "pwd1133";
- final boolean isActionLogin = true;
- //Passing data to HomeActivity
- startActivity(new HomeIntent(this, isActionLogin));
- }
- }
- public class HomeActivity extends AppCompatActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_home);
- //This is how we receive the data from SplashActivity
- //Make sure you pass getIntent() to the HomeIntent constructor
- final HomeIntent homeIntent = new HomeIntent(getIntent());
- Log.d("HomeActivity", "Is action login? " + homeIntent.isActionLogIn());
- Log.d("HomeActivity", "username: " + homeIntent.getUsername());
- Log.d("HomeActivity", "password: " + homeIntent.getPassword());
- }
- }
- Intent intent = new Intent(getActivity(), SecondActivity.class);
- intent.putExtra(Intent.EXTRA_TEXT, "my text");
- startActivity(intent);
- Intent intent = getIntent();
- String myText = intent.getExtras().getString(Intent.EXTRA_TEXT);
- static final String EXTRA_STUFF = "com.myPackageName.EXTRA_STUFF";
- Intent intent = new Intent(getActivity(), SecondActivity.class);
- intent.putExtra(EXTRA_STUFF, "my text");
- startActivity(intent);
- Intent intent = getIntent();
- String myText = intent.getExtras().getString(FirstActivity.EXTRA_STUFF);
- <string name="EXTRA_STUFF">com.myPackageName.MY_NAME</string>
- Intent intent = new Intent(getActivity(), SecondActivity.class);
- intent.putExtra(getString(R.string.EXTRA_STUFF), "my text");
- startActivity(intent);
- Intent intent = getIntent();
- String myText = intent.getExtras().getString(getString(R.string.EXTRA_STUFF));
- Intent mIntent = new Intent(FirstActivity.this, SecondActivity.class);
- mIntent.putExtra("data", data);
- startActivity(mIntent);
- public class DataHolder {
- private static DataHolder dataHolder;
- private List<Model> dataList;
- public void setDataList(List<Model>dataList) {
- this.dataList = dataList;
- }
- public List<Model> getDataList() {
- return dataList;
- }
- public synchronized static DataHolder getInstance() {
- if (dataHolder == null) {
- dataHolder = new DataHolder();
- }
- return dataHolder;
- }
- private List<Model> dataList = new ArrayList<>();
- DataHolder.getInstance().setDataList(dataList);
- private List<Model> dataList = DataHolder.getInstance().getDataList();
- $.prefs(...).put("val1", 123).put("val2", "Hello World!").put("something", 3.34);
- $.Intent().put("data", "myData").put("more", 568)...
- this.extras()
- public class GlobalClass extends Application
- {
- private float vitamin_a;
- public float getVitaminA() {
- return vitamin_a;
- }
- public void setVitaminA(float vitamin_a) {
- this.vitamin_a = vitamin_a;
- }
- }
- GlobalClass gc = (GlobalClass) getApplication();
- gc.getVitaminA()
- private static Info instance;
- private int id;
- private String name;
- //Private constructor is to disallow instances creation outside create() or getInstance() methods
- private Info() {
- }
- //Method you use to get the same information from any Activity.
- //It returns the existing Info instance, or null if not created yet.
- public static Info getInstance() {
- return instance;
- }
- //Creates a new Info instance or returns the existing one if it exists.
- public static synchronized Info create(int id, String name) {
- if (null == instance) {
- instance = new Info();
- instance.id = id;
- instance.name = name;
- }
- return instance;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement