Guest User

Untitled

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