Guest User

Untitled

a guest
Nov 17th, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.98 KB | None | 0 0
  1. Intent intent = new Intent(getBaseContext(), SignoutActivity.class);
  2. intent.putExtra("EXTRA_SESSION_ID", sessionId);
  3. startActivity(intent);
  4.  
  5. String s = getIntent().getStringExtra("EXTRA_SESSION_ID");
  6.  
  7. Intent i = new Intent(getApplicationContext(), NewActivity.class);
  8.  
  9. i.putExtra("key","value");
  10. startActivity(i);
  11.  
  12. Bundle extras = getIntent().getExtras();
  13. if (extras != null) {
  14. String value = extras.getString("key");
  15. //The key argument here must match that used in the other activity
  16. }
  17.  
  18. Intent myIntent = new Intent(this, NewActivity.class);
  19. myIntent.putExtra("firstName", "Your First Name Here");
  20. myIntent.putExtra("lastName", "Your Last Name Here");
  21. startActivity(myIntent)
  22.  
  23. protected void onCreate(Bundle savedInstanceState) {
  24. super.onCreate(savedInstanceState);
  25. setContentView(R.layout.view);
  26.  
  27. Intent intent = getIntent();
  28.  
  29. String fName = intent.getStringExtra("firstName");
  30. String lName = intent.getStringExtra("lastName");
  31. }
  32.  
  33. Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
  34. intent.putExtra("Variable name", "Value you want to pass");
  35. startActivity(intent);
  36.  
  37. long value = getIntent().getLongExtra("Variable name which you sent as an extra", defaultValue(you can give it anything));
  38.  
  39. String value = getIntent().getStringExtra("Variable name which you sent as an extra");
  40.  
  41. Boolean value = getIntent().getBooleanExtra("Variable name which you sent as an extra", defaultValue);
  42.  
  43. import android.content.Intent;
  44.  
  45. public class IntentHelper {
  46. public static final Intent createYourSpecialIntent(Intent src) {
  47. return new Intent("YourSpecialIntent").addCategory("YourSpecialCategory").putExtras(src);
  48. }
  49. }
  50.  
  51. IntentHelper.createYourSpecialIntent(getIntent());
  52.  
  53. IntentHelper.createYourSpecialIntent(getIntent()).putExtra("YOUR_FIELD_NAME", fieldValueToSave);
  54.  
  55. getIntent().getStringExtra("YOUR_FIELD_NAME");
  56.  
  57. Intent intent = new Intent(getBaseContext(), NextActivity.class);
  58. Foo foo = new Foo();
  59. intent.putExtra("foo", foo);
  60. startActivity(intent);
  61.  
  62. Foo foo = getIntent().getExtras().getParcelable("foo");
  63.  
  64. public class MyActivity extends Activity {
  65.  
  66. public static String SharedString;
  67. public static SomeObject SharedObject;
  68.  
  69. //...
  70.  
  71. Intent i = new Intent(this, ActivityTwo.class);
  72. AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete);
  73. String getrec=textView.getText().toString();
  74. Bundle bundle = new Bundle();
  75. bundle.putString(“stuff”, getrec);
  76. i.putExtras(bundle);
  77. startActivity(i);
  78.  
  79. Bundle bundle = getIntent().getExtras();
  80.  
  81. String stuff = bundle.getString(“stuff”);
  82.  
  83. String str = "My Data"; //Data you want to send
  84. Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
  85. intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  86. intent.putExtra("name",str); //Here you will add the data into intent to pass bw activites
  87. v.getContext().startActivity(intent);
  88.  
  89. import android.content.Intent;
  90.  
  91. String name = this.getIntent().getStringExtra("name");
  92.  
  93. SharedPreferences preferences = getSharedPreferences("session",getApplicationContext().MODE_PRIVATE);
  94. Editor editor = preferences.edit();
  95. editor.putString("sessionId", sessionId);
  96. editor.commit();
  97.  
  98. SharedPreferences preferences = getSharedPreferences("session", getApplicationContext().MODE_PRIVATE);
  99. String sessionId = preferences.getString("sessionId", null);
  100.  
  101. SharedPreferences settings = context.getSharedPreferences("session", Context.MODE_PRIVATE);
  102. settings.edit().clear().commit();
  103.  
  104. public class MainActivity extends AppCompatActivity {
  105.  
  106. @Override
  107. protected void onCreate(Bundle savedInstanceState) {
  108. super.onCreate(savedInstanceState);
  109. setContentView(R.layout.activity_main);
  110. }
  111.  
  112. // "Go to Second Activity" button click
  113. public void onButtonClick(View view) {
  114.  
  115. // get the text to pass
  116. EditText editText = (EditText) findViewById(R.id.editText);
  117. String textToPass = editText.getText().toString();
  118.  
  119. // start the SecondActivity
  120. Intent intent = new Intent(this, SecondActivity.class);
  121. intent.putExtra(Intent.EXTRA_TEXT, textToPass);
  122. startActivity(intent);
  123. }
  124. }
  125.  
  126. public class SecondActivity extends AppCompatActivity {
  127.  
  128. @Override
  129. protected void onCreate(Bundle savedInstanceState) {
  130. super.onCreate(savedInstanceState);
  131. setContentView(R.layout.activity_second);
  132.  
  133. // get the text from MainActivity
  134. Intent intent = getIntent();
  135. String text = intent.getStringExtra(Intent.EXTRA_TEXT);
  136.  
  137. // use the text in a TextView
  138. TextView textView = (TextView) findViewById(R.id.textView);
  139. textView.setText(text);
  140. }
  141. }
  142.  
  143. public class MainActivity extends AppCompatActivity {
  144.  
  145. private static final int SECOND_ACTIVITY_RESULT_CODE = 0;
  146.  
  147. @Override
  148. protected void onCreate(Bundle savedInstanceState) {
  149. super.onCreate(savedInstanceState);
  150. setContentView(R.layout.activity_main);
  151. }
  152.  
  153. // "Go to Second Activity" button click
  154. public void onButtonClick(View view) {
  155.  
  156. // Start the SecondActivity
  157. Intent intent = new Intent(this, SecondActivity.class);
  158. startActivityForResult(intent, SECOND_ACTIVITY_RESULT_CODE);
  159. }
  160.  
  161. // This method is called when the second activity finishes
  162. @Override
  163. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  164. super.onActivityResult(requestCode, resultCode, data);
  165.  
  166. // check that it is the SecondActivity with an OK result
  167. if (requestCode == SECOND_ACTIVITY_RESULT_CODE) {
  168. if (resultCode == RESULT_OK) {
  169.  
  170. // get String data from Intent
  171. String returnString = data.getStringExtra(Intent.EXTRA_TEXT);
  172.  
  173. // set text view with string
  174. TextView textView = (TextView) findViewById(R.id.textView);
  175. textView.setText(returnString);
  176. }
  177. }
  178. }
  179. }
  180.  
  181. public class SecondActivity extends AppCompatActivity {
  182.  
  183. @Override
  184. protected void onCreate(Bundle savedInstanceState) {
  185. super.onCreate(savedInstanceState);
  186. setContentView(R.layout.activity_second);
  187. }
  188.  
  189. // "Send text back" button click
  190. public void onButtonClick(View view) {
  191.  
  192. // get the text from the EditText
  193. EditText editText = (EditText) findViewById(R.id.editText);
  194. String stringToPassBack = editText.getText().toString();
  195.  
  196. // put the String to pass back into an Intent and close this activity
  197. Intent intent = new Intent();
  198. intent.putExtra(Intent.EXTRA_TEXT, stringToPassBack);
  199. setResult(RESULT_OK, intent);
  200. finish();
  201. }
  202. }
  203.  
  204. int n= 10;
  205. Intent in = new Intent(From_Activity.this,To_Activity.class);
  206. Bundle b1 = new Bundle();
  207. b1.putInt("integerNumber",n);
  208. in.putExtras(b1);
  209. startActivity(in);
  210.  
  211. Bundle b2 = getIntent().getExtras();
  212. int m = 0;
  213. if(b2 != null)
  214. {
  215. m = b2.getInt("integerNumber");
  216. }
  217.  
  218. i = new Intent(FirstActivity.this,SecondActivity.class);
  219. i.putExtra("key", value);
  220. startActivity(i)
  221.  
  222. Bundle bundle= getIntent().getExtras();
  223.  
  224. Intent intent = new Intent(this, Activity.class);
  225. intent.putExtra("bitmap", bitmap);
  226.  
  227. Bitmap bitmap = getIntent().getParcelableExtra("bitmap");
  228.  
  229. SecondFragment fragment = new SecondFragment();
  230. Bundle bundle = new Bundle();
  231. bundle.putParcelable("bitmap", bitmap);
  232. fragment.setArguments(bundle);
  233.  
  234. Bitmap bitmap = getArguments().getParcelable("bitmap");
  235.  
  236. Intent intent = new Intent(this, SecondActivity.class);
  237.  
  238. ByteArrayOutputStream stream = new ByteArrayOutputStream();
  239. bitmap.compress(Bitmap.CompressFormat.JPG, 100, stream);
  240. byte[] bytes = stream.toByteArray();
  241. intent.putExtra("bitmapbytes",bytes);
  242.  
  243. byte[] bytes = getIntent().getByteArrayExtra("bitmapbytes");
  244. Bitmap bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
  245.  
  246. Intent intent = new Intent(YourCurrentActivity.this, YourActivityName.class);
  247. intent.putExtra("NAme","John");
  248. intent.putExtra("Id",1);
  249. startActivity(intent);
  250.  
  251. int id = getIntent.getIntExtra("id", /* defaltvalue */ 2);
  252.  
  253. Intent i = getIntent();
  254. String name = i.getStringExtra("name");
  255.  
  256. SharedPreferences pref = myContexy.getSharedPreferences("Session
  257. Data",MODE_PRIVATE);
  258. SharedPreferences.Editor edit = pref.edit();
  259. edit.putInt("Session ID", session_id);
  260. edit.commit();
  261.  
  262. SharedPreferences pref = myContexy.getSharedPreferences("Session Data", MODE_PRIVATE);
  263. session_id = pref.getInt("Session ID", 0);
  264.  
  265. /*
  266. * If you are from transferring data from one class that doesn't
  267. * extend Activity, then you need to do something like this.
  268. */
  269.  
  270. public class abc {
  271. Context context;
  272.  
  273. public abc(Context context) {
  274. this.context = context;
  275. }
  276.  
  277. public void something() {
  278. context.startactivity(new Intent(context, anyone.class).putextra("key", value));
  279. }
  280. }
  281.  
  282. public class Info
  283. {
  284. public static int ID = 0;
  285. public static String NAME = "TEST";
  286. }
  287.  
  288. Info.ID
  289. Info.NAME
  290.  
  291. Info.ID = 5;
  292. Info.NAME = "USER!";
  293.  
  294. Intent intent = new Intent(getActivity(), SecondActivity.class);
  295. intent.putExtra(Intent.EXTRA_TEXT, "my text");
  296. startActivity(intent);
  297.  
  298. Intent intent = getIntent();
  299. String myText = intent.getExtras().getString(Intent.EXTRA_TEXT);
  300.  
  301. static final String EXTRA_STUFF = "com.myPackageName.EXTRA_STUFF";
  302.  
  303. Intent intent = new Intent(getActivity(), SecondActivity.class);
  304. intent.putExtra(EXTRA_STUFF, "my text");
  305. startActivity(intent);
  306.  
  307. Intent intent = getIntent();
  308. String myText = intent.getExtras().getString(FirstActivity.EXTRA_STUFF);
  309.  
  310. <string name="EXTRA_STUFF">com.myPackageName.MY_NAME</string>
  311.  
  312. Intent intent = new Intent(getActivity(), SecondActivity.class);
  313. intent.putExtra(getString(R.string.EXTRA_STUFF), "my text");
  314. startActivity(intent);
  315.  
  316. Intent intent = getIntent();
  317. String myText = intent.getExtras().getString(getString(R.string.EXTRA_STUFF));
  318.  
  319. Intent intent = new Intent(currentActivity.this, TargetActivity.class);
  320. intent.putExtra("booktype", "favourate");
  321. startActivity(intent);
  322.  
  323. Bundle b = getIntent().getExtras();
  324. String typesofbook = b.getString("booktype");
  325.  
  326. public class HomeIntent extends Intent {
  327.  
  328. private static final String ACTION_LOGIN = "action_login";
  329. private static final String ACTION_LOGOUT = "action_logout";
  330.  
  331. private static final String ARG_USERNAME = "arg_username";
  332. private static final String ARG_PASSWORD = "arg_password";
  333.  
  334.  
  335. public HomeIntent(Context ctx, boolean isLogIn) {
  336. this(ctx);
  337. //set action type
  338. setAction(isLogIn ? ACTION_LOGIN : ACTION_LOGOUT);
  339. }
  340.  
  341. public HomeIntent(Context ctx) {
  342. super(ctx, HomeActivity.class);
  343. }
  344.  
  345. //This will be needed for receiving data
  346. public HomeIntent(Intent intent) {
  347. super(intent);
  348. }
  349.  
  350. public void setData(String userName, String password) {
  351. putExtra(ARG_USERNAME, userName);
  352. putExtra(ARG_PASSWORD, password);
  353. }
  354.  
  355. public String getUsername() {
  356. return getStringExtra(ARG_USERNAME);
  357. }
  358.  
  359. public String getPassword() {
  360. return getStringExtra(ARG_PASSWORD);
  361. }
  362.  
  363. //To separate the params is for which action, we should create action
  364. public boolean isActionLogIn() {
  365. return getAction().equals(ACTION_LOGIN);
  366. }
  367.  
  368. public boolean isActionLogOut() {
  369. return getAction().equals(ACTION_LOGOUT);
  370. }
  371. }
  372.  
  373. public class SplashActivity extends AppCompatActivity {
  374. @Override
  375. protected void onCreate(@Nullable Bundle savedInstanceState) {
  376. super.onCreate(savedInstanceState);
  377. setContentView(R.layout.activity_splash);
  378.  
  379. String username = "phearum";
  380. String password = "pwd1133";
  381. final boolean isActionLogin = true;
  382. //Passing data to HomeActivity
  383. final HomeIntent homeIntent = new HomeIntent(this, isActionLogin);
  384. homeIntent.setData(username, password);
  385. startActivity(homeIntent);
  386.  
  387. }
  388. }
  389.  
  390. public class HomeActivity extends AppCompatActivity {
  391.  
  392. @Override
  393. protected void onCreate(Bundle savedInstanceState) {
  394. super.onCreate(savedInstanceState);
  395. setContentView(R.layout.activity_home);
  396.  
  397. //This is how we receive the data from SplashActivity
  398. //Make sure you pass getIntent() to the HomeIntent constructor
  399. final HomeIntent homeIntent = new HomeIntent(getIntent());
  400. Log.d("HomeActivity", "Is action login? " + homeIntent.isActionLogIn());
  401. Log.d("HomeActivity", "username: " + homeIntent.getUsername());
  402. Log.d("HomeActivity", "password: " + homeIntent.getPassword());
  403. }
  404. }
  405.  
  406. $.prefs(...).put("val1", 123).put("val2", "Hello World!").put("something", 3.34);
  407.  
  408. $.Intent().put("data", "myData").put("more", 568)...
  409.  
  410. this.extras()
  411.  
  412. public class GlobalClass extends Application
  413. {
  414. private float vitamin_a;
  415.  
  416.  
  417. public float getVitaminA() {
  418. return vitamin_a;
  419. }
  420.  
  421. public void setVitaminA(float vitamin_a) {
  422. this.vitamin_a = vitamin_a;
  423. }
  424. }
  425.  
  426. GlobalClass gc = (GlobalClass) getApplication();
  427.  
  428. gc.getVitaminA()
  429.  
  430. ...
  431. <application android:name="MyApplication"
  432. android:allowBackup="true"
  433. android:icon="@drawable/ic_launcher"
  434. android:label="@string/app_name"
  435. android:theme="@style/AppTheme" >
  436. ....
  437.  
  438. public class MyApplication extends Application {
  439. private MainActivity mainActivity;
  440.  
  441. @Override
  442. public void onCreate() {
  443. super.onCreate();
  444. }
  445.  
  446. public void setMainActivity(MainActivity activity) { this.mainActivity=activity; }
  447. public MainActivity getMainActivity() { return mainActivity; }
  448. }
  449.  
  450. public class MainActivity extends Activity {
  451. @Override
  452. protected void onCreate(Bundle savedInstanceState) {
  453. super.onCreate(savedInstanceState);
  454. setContentView(R.layout.activity_main);
  455. ((MyApplication)getApplication()).setMainActivity(this);
  456. }
  457. ...
  458.  
  459. }
  460.  
  461. public class MyPreferences extends PreferenceActivity
  462. implements SharedPreferences.OnSharedPreferenceChangeListener {
  463. @SuppressWarnings("deprecation")
  464. @Override
  465. public void onCreate(Bundle savedInstanceState) {
  466. super.onCreate(savedInstanceState);
  467. addPreferencesFromResource(R.xml.preferences);
  468. PreferenceManager.getDefaultSharedPreferences(this)
  469. .registerOnSharedPreferenceChangeListener(this);
  470. }
  471.  
  472. @Override
  473. public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
  474. if (!key.equals("autostart")) {
  475. ((MyApplication)getApplication()).getMainActivity().refreshUI();
  476. }
  477. }
  478. }
  479.  
  480. Intent mIntent = new Intent(FirstActivity.this, SecondActivity.class);
  481. mIntent.putExtra("data", data);
  482. startActivity(mIntent);
  483.  
  484. public class DataHolder {
  485.  
  486. private static DataHolder dataHolder;
  487. private List<Model> dataList;
  488.  
  489. public void setDataList(List<Model>dataList) {
  490. this.dataList = dataList;
  491. }
  492.  
  493. public List<Model> getDataList() {
  494. return dataList;
  495. }
  496.  
  497. public synchronized static DataHolder getInstance() {
  498. if (dataHolder == null) {
  499. dataHolder = new DataHolder();
  500. }
  501. return dataHolder;
  502. }
  503.  
  504. private List<Model> dataList = new ArrayList<>();
  505. DataHolder.getInstance().setDataList(dataList);
  506.  
  507. private List<Model> dataList = DataHolder.getInstance().getDataList();
Add Comment
Please, Sign In to add comment