Advertisement
Guest User

Untitled

a guest
Jul 4th, 2017
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.41 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. public class MyActivity extends Activity {
  58.  
  59. public static String SharedString;
  60. public static SomeObject SharedObject;
  61.  
  62. //...
  63.  
  64. Intent intent = new Intent(getBaseContext(), NextActivity.class);
  65. Foo foo = new Foo();
  66. intent.putExtra("foo", foo);
  67. startActivity(intent);
  68.  
  69. Foo foo = getIntent().getExtras().getParcelable("foo");
  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. 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. Intent intent = new Intent(currentActivity.this, TargetActivity.class);
  157. intent.putExtra("booktype", "favourate");
  158. startActivity(intent);
  159.  
  160. Bundle b = getIntent().getExtras();
  161. String typesofbook = b.getString("booktype");
  162.  
  163. SharedPreferences pref = myContexy.getSharedPreferences("Session
  164. Data",MODE_PRIVATE);
  165. SharedPreferences.Editor edit = pref.edit();
  166. edit.putInt("Session ID", session_id);
  167. edit.commit();
  168.  
  169. SharedPreferences pref = myContexy.getSharedPreferences("Session Data", MODE_PRIVATE);
  170. session_id = pref.getInt("Session ID", 0);
  171.  
  172. /*
  173. * If you are from transferring data from one class that doesn't
  174. * extend Activity, then you need to do something like this.
  175. */
  176.  
  177. public class abc {
  178. Context context;
  179.  
  180. public abc(Context context) {
  181. this.context = context;
  182. }
  183.  
  184. public void something() {
  185. context.startactivity(new Intent(context, anyone.class).putextra("key", value));
  186. }
  187. }
  188.  
  189. public class Info
  190. {
  191. public static int ID = 0;
  192. public static String NAME = "TEST";
  193. }
  194.  
  195. Info.ID
  196. Info.NAME
  197.  
  198. Info.ID = 5;
  199. Info.NAME = "USER!";
  200.  
  201. ...
  202. <application android:name="MyApplication"
  203. android:allowBackup="true"
  204. android:icon="@drawable/ic_launcher"
  205. android:label="@string/app_name"
  206. android:theme="@style/AppTheme" >
  207. ....
  208.  
  209. public class MyApplication extends Application {
  210. private MainActivity mainActivity;
  211.  
  212. @Override
  213. public void onCreate() {
  214. super.onCreate();
  215. }
  216.  
  217. public void setMainActivity(MainActivity activity) { this.mainActivity=activity; }
  218. public MainActivity getMainActivity() { return mainActivity; }
  219. }
  220.  
  221. public class MainActivity extends Activity {
  222. @Override
  223. protected void onCreate(Bundle savedInstanceState) {
  224. super.onCreate(savedInstanceState);
  225. setContentView(R.layout.activity_main);
  226. ((MyApplication)getApplication()).setMainActivity(this);
  227. }
  228. ...
  229.  
  230. }
  231.  
  232. public class MyPreferences extends PreferenceActivity
  233. implements SharedPreferences.OnSharedPreferenceChangeListener {
  234. @SuppressWarnings("deprecation")
  235. @Override
  236. public void onCreate(Bundle savedInstanceState) {
  237. super.onCreate(savedInstanceState);
  238. addPreferencesFromResource(R.xml.preferences);
  239. PreferenceManager.getDefaultSharedPreferences(this)
  240. .registerOnSharedPreferenceChangeListener(this);
  241. }
  242.  
  243. @Override
  244. public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
  245. if (!key.equals("autostart")) {
  246. ((MyApplication)getApplication()).getMainActivity().refreshUI();
  247. }
  248. }
  249. }
  250.  
  251. public class HomeIntent extends Intent {
  252.  
  253. private static final String ACTION_LOGIN = "action_login";
  254. private static final String ACTION_LOGOUT = "action_logout";
  255.  
  256. private static final String ARG_USERNAME = "arg_username";
  257. private static final String ARG_PASSWORD = "arg_password";
  258.  
  259.  
  260. public HomeIntent(Context ctx, boolean isLogIn) {
  261. this(ctx);
  262. //set action type
  263. setAction(isLogIn ? ACTION_LOGIN : ACTION_LOGOUT);
  264. }
  265.  
  266. public HomeIntent(Context ctx) {
  267. super(ctx, HomeActivity.class);
  268. }
  269.  
  270. //This will be needed for receiving data
  271. public HomeIntent(Intent intent) {
  272. super(intent);
  273. }
  274.  
  275. public void setUserName(String userName, String password) {
  276. putExtra(ARG_USERNAME, userName);
  277. putExtra(ARG_PASSWORD, password);
  278. }
  279.  
  280. public String getUsername() {
  281. return getStringExtra(ARG_USERNAME);
  282. }
  283.  
  284. public String getPassword() {
  285. return getStringExtra(ARG_PASSWORD);
  286. }
  287.  
  288. //To separate the params is for which action, we should create action
  289. public boolean isActionLogIn() {
  290. return getAction().equals(ACTION_LOGIN);
  291. }
  292.  
  293. public boolean isActionLogOut() {
  294. return getAction().equals(ACTION_LOGOUT);
  295. }
  296. }
  297.  
  298. public class SplashActivity extends AppCompatActivity {
  299. @Override
  300. protected void onCreate(@Nullable Bundle savedInstanceState) {
  301. super.onCreate(savedInstanceState);
  302. setContentView(R.layout.activity_splash);
  303.  
  304. String username = "phearum";
  305. String password = "pwd1133";
  306. final boolean isActionLogin = true;
  307. //Passing data to HomeActivity
  308. startActivity(new HomeIntent(this, isActionLogin));
  309.  
  310. }
  311. }
  312.  
  313. public class HomeActivity extends AppCompatActivity {
  314.  
  315. @Override
  316. protected void onCreate(Bundle savedInstanceState) {
  317. super.onCreate(savedInstanceState);
  318. setContentView(R.layout.activity_home);
  319.  
  320. //This is how we receive the data from SplashActivity
  321. //Make sure you pass getIntent() to the HomeIntent constructor
  322. final HomeIntent homeIntent = new HomeIntent(getIntent());
  323. Log.d("HomeActivity", "Is action login? " + homeIntent.isActionLogIn());
  324. Log.d("HomeActivity", "username: " + homeIntent.getUsername());
  325. Log.d("HomeActivity", "password: " + homeIntent.getPassword());
  326. }
  327. }
  328.  
  329. Intent intent = new Intent(getActivity(), SecondActivity.class);
  330. intent.putExtra(Intent.EXTRA_TEXT, "my text");
  331. startActivity(intent);
  332.  
  333. Intent intent = getIntent();
  334. String myText = intent.getExtras().getString(Intent.EXTRA_TEXT);
  335.  
  336. static final String EXTRA_STUFF = "com.myPackageName.EXTRA_STUFF";
  337.  
  338. Intent intent = new Intent(getActivity(), SecondActivity.class);
  339. intent.putExtra(EXTRA_STUFF, "my text");
  340. startActivity(intent);
  341.  
  342. Intent intent = getIntent();
  343. String myText = intent.getExtras().getString(FirstActivity.EXTRA_STUFF);
  344.  
  345. <string name="EXTRA_STUFF">com.myPackageName.MY_NAME</string>
  346.  
  347. Intent intent = new Intent(getActivity(), SecondActivity.class);
  348. intent.putExtra(getString(R.string.EXTRA_STUFF), "my text");
  349. startActivity(intent);
  350.  
  351. Intent intent = getIntent();
  352. String myText = intent.getExtras().getString(getString(R.string.EXTRA_STUFF));
  353.  
  354. Intent mIntent = new Intent(FirstActivity.this, SecondActivity.class);
  355. mIntent.putExtra("data", data);
  356. startActivity(mIntent);
  357.  
  358. public class DataHolder {
  359.  
  360. private static DataHolder dataHolder;
  361. private List<Model> dataList;
  362.  
  363. public void setDataList(List<Model>dataList) {
  364. this.dataList = dataList;
  365. }
  366.  
  367. public List<Model> getDataList() {
  368. return dataList;
  369. }
  370.  
  371. public synchronized static DataHolder getInstance() {
  372. if (dataHolder == null) {
  373. dataHolder = new DataHolder();
  374. }
  375. return dataHolder;
  376. }
  377.  
  378. private List<Model> dataList = new ArrayList<>();
  379. DataHolder.getInstance().setDataList(dataList);
  380.  
  381. private List<Model> dataList = DataHolder.getInstance().getDataList();
  382.  
  383. $.prefs(...).put("val1", 123).put("val2", "Hello World!").put("something", 3.34);
  384.  
  385. $.Intent().put("data", "myData").put("more", 568)...
  386.  
  387. this.extras()
  388.  
  389. public class GlobalClass extends Application
  390. {
  391. private float vitamin_a;
  392.  
  393.  
  394. public float getVitaminA() {
  395. return vitamin_a;
  396. }
  397.  
  398. public void setVitaminA(float vitamin_a) {
  399. this.vitamin_a = vitamin_a;
  400. }
  401. }
  402.  
  403. GlobalClass gc = (GlobalClass) getApplication();
  404.  
  405. gc.getVitaminA()
  406.  
  407. private static Info instance;
  408. private int id;
  409. private String name;
  410.  
  411. //Private constructor is to disallow instances creation outside create() or getInstance() methods
  412. private Info() {
  413.  
  414. }
  415.  
  416. //Method you use to get the same information from any Activity.
  417. //It returns the existing Info instance, or null if not created yet.
  418. public static Info getInstance() {
  419. return instance;
  420. }
  421.  
  422. //Creates a new Info instance or returns the existing one if it exists.
  423. public static synchronized Info create(int id, String name) {
  424.  
  425. if (null == instance) {
  426. instance = new Info();
  427. instance.id = id;
  428. instance.name = name;
  429. }
  430. return instance;
  431. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement