in my project I have static member as User.current.
If I run the app after it terminates in background, User.current will be null. I get user from server on splash screen.
I want to start splash screen when I run terminated application.
How I can solve this problem?
If you call a server, you probably use an Async task. In Async task
...
#Override
protected Boolean doInBackground(Void... params) {
...
value = jsonObj.getString("value");
return true;
}
protected void onPostExecute(Boolean iHaveValue) {
if(iHaveValue){
// use case 1 or 2
// 1
Intent i = new Intent(getApplicationContext(), MainActivity.class);
i.putExtra("value", value);
startActivity(i);
// 2
SharedPreferences sharedpreferences = getSharedPreferences("user", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString("value", value);
editor.commit();
Intent i = new Intent(getApplicationContext(), MainActivity.class);
startActivity(i);
}
}
You get the user on the doInBackground(Void... params). And in the onPostExecute(Boolean iHaveValue) method you simply go to the MainActivity.
You can use the intent to put the value and receive on MainActivity (1) or use a shared preference (2)
(1)
in SplashScreen
Intent i = new Intent(getApplicationContext(), MainActivity.class);
i.putExtra("value", value);
startActivity(i);
in MainActivity
Intent editIntent = new Intent(getApplicationContext(), Splashscreen.class);
(2)
in SplashScreen
SharedPreferences sharedpreferences = getSharedPreferences("user", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString("value", value);
editor.commit();
in MainActivity
SharedPreferences prefs = getSharedPreferences("user", MODE_PRIVATE);
value = prefs.getString("value", "null");
If you need use this value for multiple locations, sharedPreferences helps a lot (case 2). If not, use putExtra to send info to MainActivity (case 1).
Related
In my app urls are getting from the firebase database . I want to send these url to the another activity. how can i pass the url (Uri webpage) to the
viewer class.Please tell me how to do it.
Use intent in first activity like this:
Intent intent = new Intent(CurrentActivity.this, NextActivity.class);
intent.putExtra("key", value);
CurrentActivity.this.startActivity(intent);
And retrieve it on the second activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
Intent intent = getIntent();
String value = intent.getStringExtra("key");
}
you can use this SharedPreferences
SharedPreferences sharedPref = getSharedPreferences("key",Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("key1","your uri");
editor.apply();
if you want get value in new ativity
SharedPreferences sharedPref = getSharedPreferences("key",Context.MODE_PRIVATE);
String uri=sharedPref.getString("key1","default value if value is null");
keys values must be same
You can also send String Array via intent from one activity to another.
In sender's activity, you can add URL string array into intent.
Intent intent = new Intent(this, MainActivity.class);
String[] urls = new String[] {
"https://google.com",
"https://stackoverflow.com"
};
intent.putExtra("urls", urls);
startActivity(intent);
In MainActivity (Receiver), you can get String array from intent.
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] urls = getIntent().getStringArrayExtra("urls");
}
}
Hope it will be helpful.
I'm a kinda beginner...and i am working on a project that have a login activity and i want to skip the login activity in relaunching... after a successful login.
Pleas define it well if possible!
Maybe, It's help you.
LoginActivity.java
public void onCreate(){
/*your code in top*/
SharedPreferences prefs= this.getSharedPreferences("APP", Context.MODE_PRIVATE);
if(press.contains("loggedIn")){
startActivity(new Intent(this, NextActivity.class));
finish();
}
/*END*/
}
use shared preferences set a tag after login lets loggedin=true and put a check onCreate() if preferences values is true direct it to next activity
Store a variable in Shared Preferences off the app and check it in oncreate of Login Activity if the value is saved move to main activity using intent.
Setting values in Preference:
// MY_PREFS_NAME - a static String variable like:
public static final String MY_PREFS_NAME = "MyPrefsFile";
SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME,
MODE_PRIVATE).edit();
editor.putString("login", "true");
editor.apply();
Retrieve data from preference in Oncreate on Login Activity:
SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
String restoredText = prefs.getString("login", null);
if (restoredText.equals("true")) {
Intent i=new Intent();
i.startActivity(this,Activity_you_want_to_show.class);
finish();
}
I have created android app shortcut on home screen using code from main activity
Shortcut got created successfully and i am able to launch the application.
Functionality is also working fine but when I click on shortcut then getting an error that app not found.
Please review my code and guide me how can I solve this problem.
Here is my main activity code:-
Context context = MainActivity.this;
SharedPreferences sharedPreferences;
boolean isAppInstalled = false;
Here is my onCreate code
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
isAppInstalled= sharedPreferences.getBoolean("isAppInstalled", false);
if(isAppInstalled==false){
Intent intent1 = new Intent(getApplicationContext(),MainActivity.class);
intent1.setAction(Intent.ACTION_MAIN);
Intent intent2 = new Intent();
intent2.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent1);
intent2.putExtra(Intent.EXTRA_SHORTCUT_NAME,"My Application");
intent2.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,Intent.ShortcutIconResource.fromContext(getApplicationContext(),R.drawable.ic_launcher));
intent2.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
getApplicationContext().sendBroadcast(intent2);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("isAppInstalled",true);
editor.commit();
}
No anyone help me in this.
well i solved myself thank for all and here is my problem
I created splash activity for my app that why it will use in my code
mean change MainActivity with SplashActivity
Here is correct Code
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
isAppInstalled= sharedPreferences.getBoolean("isAppInstalled", false);
if(isAppInstalled==false){
Intent intent1 = new Intent(getApplicationContext(),SplashActivity.class);
intent1.setAction(Intent.ACTION_MAIN);
Intent intent2 = new Intent();
intent2.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent1);
intent2.putExtra(Intent.EXTRA_SHORTCUT_NAME,"My Application");
intent2.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,Intent.ShortcutIconResource.fromContext(getApplicationContext(),R.drawable.ic_launcher));
intent2.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
getApplicationContext().sendBroadcast(intent2);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("isAppInstalled",true);
editor.commit();
}
I have two activities namely Registration and Login where I defined a Shared Preferences in the Registration class. On the Registration page, if you click on the button that takes you to the Login Activity from the Registration Activity. I am storing a Shared Preferences value, so that on launching the app the second time, let the app immediately go to the LoginActivity instead of opening the first Activity which is Registration Activity. Here is my SharedPreferences class called Session.
private SharedPreferences prefs;
public Session(Context cntx) {
// TODO Auto-generated constructor stub
prefs = PreferenceManager.getDefaultSharedPreferences(cntx);
}
public void setusename(String usename) {
SharedPreferences.Editor editor = prefs.edit();
editor.putString("username", usename).commit();
editor.commit();
public Boolean contKey(String key){
if (prefs.contains(key)){
return true;
}else{
return false;
}
}
Here is the button in the RegstrationActivity that stores the SharedPreferences value before going to the LoginActivity so that on launch the second time, it opens the LoginActivity class
loginLink.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
session = new Session(getApplicationContext());
session.setusename("hello123");
Intent intent = new Intent(getApplication(),ActivityLogin.class);
startActivity(intent);
finish();
}
});
I have defined this in the onCreate method of RegistrationActivity class to switch to the ActivityLogin screen on next time the app is launched
System.out.println("it got here...2");
try {
if (session.contKey("username")) {
System.out.println("it got here...");
Intent intent = new Intent(getApplication(), ActivityLogin.class);
startActivity(intent);
overridePendingTransition(R.anim.enter, R.anim.exit);
finish();
} else {
System.out.println("it got here...3");
Intent intent = new Intent(getApplication(), ActivityRegistration.class);
startActivity(intent);
finish();
overridePendingTransition(R.anim.enter, R.anim.exit);
}
}catch(Exception ex){
}
Please why is my Shared Preferences not switching to the LoginActivity the second time the is launched. Kindly assist
TRy this
private SharedPreferences prefs;
public Session(Context cntx) {
// TODO Auto-generated constructor stub
prefs = PreferenceManager.getDefaultSharedPreferences(cntx);
}
public void setusename(String usename) {
SharedPreferences.Editor editor = prefs.edit();
editor.putString("username", usename);
editor.commit();
}
}
public String getusename() {
return prefs.getString("username","");
}
}
than use this way to get value SharedPreferences
session = new Session(getApplicationContext());
session.setusename("hello123");
String username=session.getusename();
Sample code
try {
if (!session.getusename().equals("")) {
System.out.println("it got here...");
Intent intent = new Intent(getApplication(), ActivityLogin.class);
startActivity(intent);
overridePendingTransition(R.anim.enter, R.anim.exit);
finish();
} else {
System.out.println("it got here...3");
Intent intent = new Intent(getApplication(), ActivityRegistration.class);
startActivity(intent);
finish();
overridePendingTransition(R.anim.enter, R.anim.exit);
}
}catch(Exception ex){
}
Instead of default shared preference, you can define a private one with your desired name in the Activity A..like as follows..
SharedPreferences preferences = getSharedPreferences("myPreferences",0);
SharedPreferences.Editor editor= preferences.edit();
editor.putString("userName","raju");
editor.commit();
If you want to retrieve that value from shared preferences in Activty A(I mean in the same activity), you can use following code..
String user_name= preferences.getString("userName","N/A");
or else in Activity B(I mean in some other activty), this time you don't need editor(for retrieval)..the code will be as follows
SharedPreferences preferences = getSharedPreferences("myPreferences",0);
String user_name= preferences.getString("userName","N/A");
I want to pass info as shown in this picture screen description
I know that my code is working, becuase with one Intent it works perfectley. However, when I try to put two or more Intents it seems to get messed up. I also checked here to find a sutibale solution, but I don't think I can use it the same way. Thanks in advance
--update--
still dosen't work
i did this on my saving side
Intent saveIntent = new Intent(this, MainActivity.class);
SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
editor.putString("time", displayTime.getText().toString());
editor.commit();
startActivity(saveIntent);
and this on my reciving side
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
String time = preferences.getString("time",null);
if (time != null)
getTime.setText(time);
Main Activity:
private Button createNewEvent;
private Button showMyEvents;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
createNewEvent = (Button) findViewById(R.id.bCreateNewEvent);
showMyEvents = (Button) findViewById(R.id.bShowMyEvents);
}
public void buttonOnClick(View v) {
switch (v.getId()) {
case R.id.bCreateNewEvent:
Intent createNewEventIntent = new Intent(this, CreateNewEventActivity.class);
startActivity(createNewEventIntent);
break;
case R.id.bShowMyEvents:
Intent myEventsIntent = new Intent(this,MyEventsActivity.class);
startActivity(myEventsIntent);
break;
}
}
}
screen 1 save button:
case R.id.bSaveNewEvent:
Intent putIntent = new Intent(getApplicationContext(),MyEventsActivity.class);
// String text = displayTime.getText().toString();
putIntent.putExtra("time",displayTime.getText().toString());
Intent saveIntent = new Intent(this, MainActivity.class);
startActivity(saveIntent);
startActivity(putIntent);
screen 2 getExtras
public class MyEventsActivity extends AppCompatActivity {
TextView getTime;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_events);
getTime = (TextView) findViewById(R.id.tvGetTime);
Intent in = getIntent();
String name = in.getStringExtra("time");
getTime.setText(name);
/* Bundle extras = getIntent().getExtras();
if (extras != null){
getTime.setText(extras.getString("time"));
}*/
If you are trying to save data that will be need to be used in multiple other activities, SharedPreferences will be the easiest solution.
https://developer.android.com/training/basics/data-storage/shared-preferences.html
When you want to save a value, you would use:
SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
editor.putString("time", displayTime.getText().toString());
editor.commit();
Then when you want to get the preference later on you would use:
SharedPreferences preferences = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
String time = preferences.getString("time", "");