Advertisement
Guest User

Untitled

a guest
Mar 15th, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.13 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. String str = "My Data"; //Data you want to send
  72. Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
  73. intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  74. intent.putExtra("name",str); //Here you will add the data into intent to pass bw activites
  75. v.getContext().startActivity(intent);
  76.  
  77. import android.content.Intent;
  78.  
  79. String name = this.getIntent().getStringExtra("name");
  80.  
  81. Intent i = new Intent(this, ActivityTwo.class);
  82. AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete);
  83. String getrec=textView.getText().toString();
  84. Bundle bundle = new Bundle();
  85. bundle.putString(“stuff”, getrec);
  86. i.putExtras(bundle);
  87. startActivity(i);
  88.  
  89. Bundle bundle = getIntent().getExtras();
  90.  
  91. String stuff = bundle.getString(“stuff”);
  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. int n= 10;
  105. Intent in = new Intent(From_Activity.this,To_Activity.class);
  106. Bundle b1 = new Bundle();
  107. b1.putInt("integerNumber",n);
  108. in.putExtras(b1);
  109. startActivity(in);
  110.  
  111. Bundle b2 = getIntent().getExtras();
  112. int m = 0;
  113. if(b2 != null)
  114. {
  115. m = b2.getInt("integerNumber");
  116. }
  117.  
  118. i = new Intent(FirstActivity.this,SecondActivity.class);
  119. i.putExtra("key", value);
  120. startActivity(i)
  121.  
  122. Bundle bundle= getIntent().getExtras();
  123.  
  124. Intent intent = new Intent(this, Activity.class);
  125. intent.putExtra("bitmap", bitmap);
  126.  
  127. Bitmap bitmap = getIntent().getParcelableExtra("bitmap");
  128.  
  129. SecondFragment fragment = new SecondFragment();
  130. Bundle bundle = new Bundle();
  131. bundle.putParcelable("bitmap", bitmap);
  132. fragment.setArguments(bundle);
  133.  
  134. Bitmap bitmap = getArguments().getParcelable("bitmap");
  135.  
  136. Intent intent = new Intent(this, SecondActivity.class);
  137.  
  138. ByteArrayOutputStream stream = new ByteArrayOutputStream();
  139. bitmap.compress(Bitmap.CompressFormat.JPG, 100, stream);
  140. byte[] bytes = stream.toByteArray();
  141. intent.putExtra("bitmapbytes",bytes);
  142.  
  143. byte[] bytes = getIntent().getByteArrayExtra("bitmapbytes");
  144. Bitmap bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
  145.  
  146. Intent intent = new Intent(YourCurrentActivity.this, YourActivityName.class);
  147. intent.putExtra("NAme","John");
  148. intent.putExtra("Id",1);
  149. startActivity(intent);
  150.  
  151. int id = getIntent.getIntExtra("id", /* defaltvalue */ 2);
  152.  
  153. Intent i = getIntent();
  154. String name = i.getStringExtra("name");
  155.  
  156. /*
  157. * If you are from transferring data from one class that doesn't
  158. * extend Activity, then you need to do something like this.
  159. */
  160.  
  161. public class abc {
  162. Context context;
  163.  
  164. public abc(Context context) {
  165. this.context = context;
  166. }
  167.  
  168. public void something() {
  169. context.startactivity(new Intent(context, anyone.class).putextra("key", value));
  170. }
  171. }
  172.  
  173. public class Info
  174. {
  175. public static int ID = 0;
  176. public static String NAME = "TEST";
  177. }
  178.  
  179. Info.ID
  180. Info.NAME
  181.  
  182. Info.ID = 5;
  183. Info.NAME = "USER!";
  184.  
  185. ...
  186. <application android:name="MyApplication"
  187. android:allowBackup="true"
  188. android:icon="@drawable/ic_launcher"
  189. android:label="@string/app_name"
  190. android:theme="@style/AppTheme" >
  191. ....
  192.  
  193. public class MyApplication extends Application {
  194. private MainActivity mainActivity;
  195.  
  196. @Override
  197. public void onCreate() {
  198. super.onCreate();
  199. }
  200.  
  201. public void setMainActivity(MainActivity activity) { this.mainActivity=activity; }
  202. public MainActivity getMainActivity() { return mainActivity; }
  203. }
  204.  
  205. public class MainActivity extends Activity {
  206. @Override
  207. protected void onCreate(Bundle savedInstanceState) {
  208. super.onCreate(savedInstanceState);
  209. setContentView(R.layout.activity_main);
  210. ((MyApplication)getApplication()).setMainActivity(this);
  211. }
  212. ...
  213.  
  214. }
  215.  
  216. public class MyPreferences extends PreferenceActivity
  217. implements SharedPreferences.OnSharedPreferenceChangeListener {
  218. @SuppressWarnings("deprecation")
  219. @Override
  220. public void onCreate(Bundle savedInstanceState) {
  221. super.onCreate(savedInstanceState);
  222. addPreferencesFromResource(R.xml.preferences);
  223. PreferenceManager.getDefaultSharedPreferences(this)
  224. .registerOnSharedPreferenceChangeListener(this);
  225. }
  226.  
  227. @Override
  228. public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
  229. if (!key.equals("autostart")) {
  230. ((MyApplication)getApplication()).getMainActivity().refreshUI();
  231. }
  232. }
  233. }
  234.  
  235. public class HomeIntent extends Intent {
  236.  
  237. private static final String ACTION_LOGIN = "action_login";
  238. private static final String ACTION_LOGOUT = "action_logout";
  239.  
  240. private static final String ARG_USERNAME = "arg_username";
  241. private static final String ARG_PASSWORD = "arg_password";
  242.  
  243.  
  244. public HomeIntent(Context ctx, boolean isLogIn) {
  245. this(ctx);
  246. //set action type
  247. setAction(isLogIn ? ACTION_LOGIN : ACTION_LOGOUT);
  248. }
  249.  
  250. public HomeIntent(Context ctx) {
  251. super(ctx, HomeActivity.class);
  252. }
  253.  
  254. //This will be needed for receiving data
  255. public HomeIntent(Intent intent) {
  256. super(intent);
  257. }
  258.  
  259. public void setUserName(String userName, String password) {
  260. putExtra(ARG_USERNAME, userName);
  261. putExtra(ARG_PASSWORD, password);
  262. }
  263.  
  264. public String getUsername() {
  265. return getStringExtra(ARG_USERNAME);
  266. }
  267.  
  268. public String getPassword() {
  269. return getStringExtra(ARG_PASSWORD);
  270. }
  271.  
  272. //To separate the params is for which action, we should create action
  273. public boolean isActionLogIn() {
  274. return getAction().equals(ACTION_LOGIN);
  275. }
  276.  
  277. public boolean isActionLogOut() {
  278. return getAction().equals(ACTION_LOGOUT);
  279. }
  280. }
  281.  
  282. public class SplashActivity extends AppCompatActivity {
  283. @Override
  284. protected void onCreate(@Nullable Bundle savedInstanceState) {
  285. super.onCreate(savedInstanceState);
  286. setContentView(R.layout.activity_splash);
  287.  
  288. String username = "phearum";
  289. String password = "pwd1133";
  290. final boolean isActionLogin = true;
  291. //Passing data to HomeActivity
  292. startActivity(new HomeIntent(this, isActionLogin));
  293.  
  294. }
  295. }
  296.  
  297. public class HomeActivity extends AppCompatActivity {
  298.  
  299. @Override
  300. protected void onCreate(Bundle savedInstanceState) {
  301. super.onCreate(savedInstanceState);
  302. setContentView(R.layout.activity_home);
  303.  
  304. //This is how we receive the data from SplashActivity
  305. //Make sure you pass getIntent() to the HomeIntent constructor
  306. final HomeIntent homeIntent = new HomeIntent(getIntent());
  307. Log.d("HomeActivity", "Is action login? " + homeIntent.isActionLogIn());
  308. Log.d("HomeActivity", "username: " + homeIntent.getUsername());
  309. Log.d("HomeActivity", "password: " + homeIntent.getPassword());
  310. }
  311. }
  312.  
  313. Intent intent = new Intent(getActivity(), SecondActivity.class);
  314. intent.putExtra(Intent.EXTRA_TEXT, "my text");
  315. startActivity(intent);
  316.  
  317. Intent intent = getIntent();
  318. String myText = intent.getExtras().getString(Intent.EXTRA_TEXT);
  319.  
  320. static final String EXTRA_STUFF = "com.myPackageName.EXTRA_STUFF";
  321.  
  322. Intent intent = new Intent(getActivity(), SecondActivity.class);
  323. intent.putExtra(EXTRA_STUFF, "my text");
  324. startActivity(intent);
  325.  
  326. Intent intent = getIntent();
  327. String myText = intent.getExtras().getString(FirstActivity.EXTRA_STUFF);
  328.  
  329. <string name="EXTRA_STUFF">com.myPackageName.MY_NAME</string>
  330.  
  331. Intent intent = new Intent(getActivity(), SecondActivity.class);
  332. intent.putExtra(getString(R.string.EXTRA_STUFF), "my text");
  333. startActivity(intent);
  334.  
  335. Intent intent = getIntent();
  336. String myText = intent.getExtras().getString(getString(R.string.EXTRA_STUFF));
  337.  
  338. Intent mIntent = new Intent(FirstActivity.this, SecondActivity.class);
  339. mIntent.putExtra("data", data);
  340. startActivity(mIntent);
  341.  
  342. public class DataHolder {
  343.  
  344. private static DataHolder dataHolder;
  345. private List<Model> dataList;
  346.  
  347. public void setDataList(List<Model>dataList) {
  348. this.dataList = dataList;
  349. }
  350.  
  351. public List<Model> getDataList() {
  352. return dataList;
  353. }
  354.  
  355. public synchronized static DataHolder getInstance() {
  356. if (dataHolder == null) {
  357. dataHolder = new DataHolder();
  358. }
  359. return dataHolder;
  360. }
  361.  
  362. private List<Model> dataList = new ArrayList<>();
  363. DataHolder.getInstance().setDataList(dataList);
  364.  
  365. private List<Model> dataList = DataHolder.getInstance().getDataList();
  366.  
  367. $.prefs(...).put("val1", 123).put("val2", "Hello World!").put("something", 3.34);
  368.  
  369. $.Intent().put("data", "myData").put("more", 568)...
  370.  
  371. this.extras()
  372.  
  373. public class GlobalClass extends Application
  374. {
  375. private float vitamin_a;
  376.  
  377.  
  378. public float getVitaminA() {
  379. return vitamin_a;
  380. }
  381.  
  382. public void setVitaminA(float vitamin_a) {
  383. this.vitamin_a = vitamin_a;
  384. }
  385. }
  386.  
  387. GlobalClass gc = (GlobalClass) getApplication();
  388.  
  389. gc.getVitaminA()
  390.  
  391. private static Info instance;
  392. private int id;
  393. private String name;
  394.  
  395. //Private constructor is to disallow instances creation outside create() or getInstance() methods
  396. private Info() {
  397.  
  398. }
  399.  
  400. //Method you use to get the same information from any Activity.
  401. //It returns the existing Info instance, or null if not created yet.
  402. public static Info getInstance() {
  403. return instance;
  404. }
  405.  
  406. //Creates a new Info instance or returns the existing one if it exists.
  407. public static synchronized Info create(int id, String name) {
  408.  
  409. if (null == instance) {
  410. instance = new Info();
  411. instance.id = id;
  412. instance.name = name;
  413. }
  414. return instance;
  415. }
  416.  
  417. //Serializable class
  418.  
  419. public class YourClass implements Serializable {
  420. public long session_id = 0;
  421. }
  422.  
  423. Intent mIntent = new Intent(getApplicationContext(), LogoutActivity.class);
  424. mIntent.putExtra("session_id", session_id);
  425. startActivity(mIntent);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement