I'm developing an app for Android using Android studio. I have two activities; the first one to configure the app (it must appear only the first time that app is launched), the second activity is to log in to the app.
The problem is: I want to show the "config activity" at the first launch, and after that the "login activity" must appear, but I don't know how to do this. I tried to put a conditional into the "config activity" to force the second (login) to put it to work. But it doesn't work.
Can you explain me some things about this topic?
Yout main Activity, in the onCreate() method, must use a preference to see if running for the first time. If so, it should launch the config activity and also call finish(). The user won't notice that the main activity didn't launch.
Something like this:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
if(prefs.getBoolean("first_time", true))
{
SharedPreferences.Editor editor = prefs.edit()
.putBoolean("first_time", false);
editor.commit();
finish();
// launch the config activity
startActivity(new Intent(this, ConfigActivity.class));
}
Related
I have 5 different activities in my app, i enable the user to navigate between those activities by clicking buttons. The main activity has a RecyclerView in it (used to make a feed). the other activities are used for search, profile, notifications, etc. The user opens the app, the main activity is displayed. The user can scroll through the feed (which is the RecyclerView) in the main activity, now the user decides to go to other activities, search, profile, etc. Now, when the user clicks on the button that navigates to the main activity, I want to open back the main activity and display the recycler view at the same scrolling position and with the same items as was when the user just left it off.
Note: I'm not talking here about pressing the back button or using any goBack functions. I want to enable the user to use the main activity and visit all other activities and after all of that he will be able to come back to the main activity and find the feed exactly as he left it off.
In every activity I have five navigation buttons that allowing the user to go from every activity to every activity: click on profileBtn will open the profile activity, etc.
There is a way in which i can back up the state of the recycler view in the main activity before the user wants to go to other activities and restore it when the user clicks on the homeBtn?
Code:
HomeActivity.java:
public void profileBtn_OnClick(android.view.View view)
{
Intent intent = new Intent(getBaseContext(), ProfileActivity.class);
startActivity(intent);
}
ProfileActivity.java, SerachActivity.java, NotificationsActivity.java:
public void homeBtn_OnClick(android.view.View view)
{
/* HERE I WANT TO OPEN BACK THE HOME ACTIVITY AND DISPLAY THE FEED
EXACTLY AS IT WAS WHEN THE USER LEFT IT, HOW DO I DO THAT? */
Intent intent = new Intent(getBaseContext(), HomeActivity.class);
startActivity(intent);
}
You can use the launchMode="singleTask".
The system creates the activity at the root of a new task and routes
the intent to it. However, if an instance of the activity already
exists, the system routes the intent to the existing instance through
a call to its onNewIntent() method, rather than creating a new one.
You can get more information about launchMode here: https://developer.android.com/guide/topics/manifest/activity-element
android:launchMode=["standard" | "singleTop" | "singleTask" | "singleInstance"]
<activity
android:name=".ui.HomeActivity"
android:launchMode="singleTask" />
Please do NOT use the special launch mode singleTask for this purpose. It causes more problems than it solves. These launch modes have complex side-effects that are not obvious and will cause you more problems later.
Your problem is simple to solve using standard Android behaviour. Change your method to look like this:
public void homeBtn_OnClick(android.view.View view)
{
Intent intent = new Intent(getBaseContext(), HomeActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
}
The flag CLEAR_TOP tells Android to remove all activities from the task that are on top of the target Activity (in this case the target Activity is HomeActivity.
The flag SINGLE_TOP tells Android not to recreate HomeActivity, but to reuse the existing instance of it.
I have a music streaming app that has free features as well as paid features. The main activity offers the option to login/subscribe or try free. If user selects "Login/subscribe" then they are taken to the login screen where they can login or create an accout. However, if user selects "Try free" then they are taken to the library screen with limited functions. The way it is right now, if they select "Try free", then every time they open the app it asks them again to Login or Try free.
I want to change that so that, once they select "Try free" option they never see the options again but are taken to the library screen with limited functions.
So, I set HomeActivity, which is the activity I want to use every time after the first launch, as the launch option. That works great.
Then I added the following code to onCreate in HomeActivity.java
The idea is that on first launch it will lauch MainActivity, then set the Boolean isFirstRun to false, and never launch MainActivity again.
But, it's not working. Seems like it is setting isFirstRun back to true every time. How do I fix that?
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Boolean isFirstRun = getSharedPreferences("PREFERENCE", MODE_PRIVATE)
.getBoolean("isFirstRun", true);
if (isFirstRun) {
//show start activity
startActivity(new Intent(this, MainActivity.class));
Toast.makeText(this, "First Run", Toast.LENGTH_LONG)
.show();
}
getSharedPreferences("PREFERENCE", MODE_PRIVATE).edit()
.putBoolean("isFirstRun", false).commit();
I figured it out myself. It was my own fault.
Though I had specified a different activity to launch under Run>Edit Configuration, I had not set an intent filter in my mainifest file for the HomeActivity.....
Live and learn I guess
So I'm using Java in Android Studio and have come across a problem.
Essentially I have a value in my MainActivity, and when the user is in the EditActivity, they can edit this value. I do this by using putExtra for myValue when transferring from the MainActivity to the EditActivity, and when transferring back, the newly updated variable is named myNewValue. Essentially what I want is:
if(MainActivity is started via app launching){
textView.setText(myValue)
}else if(MainActivity is started via intent from EditActivity){
textView.setText(myNewValue)
}
In your First Screen (splash screen) . start your intent like this
intent.putextra("check_this","mainactivity");
intent.putextra(""your_value","abcd");
in your EditActivity.
intent.putextra("check_this","editactivity");
intent.putextra(""your_new_value","abcdefg");
So in your CurrentScreen.
Bundle b = getIntent.getExtra();
if(b.getString("check_this").equals("mainactivity")){
textView.setText(myValue). // this screen is opened from splash
}else{
textView.setText(myNewValue). // this is from editactivity
}
If I'm bringing Android activities from the stack to the front, how do I refresh them? So to run onCreate again etc.
My code below, in conjunction with setting activities in the Android manifest to android:launchMode="singleTask" allows me to initiate an activity if that activity is not already active within the stack, if it is active within the stack it is then brought to the front.
How do I then, if the activity is brought to the front refresh it so that onCreate is ran again etc.
Intent intent = new Intent(myActivity.this,
myActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
I think FLAG_ACTIVITY_CLEAR_TOP will resolved your problem:
Intent intent = new Intent(myActivity.this, myActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
I don't think there is an explicit way to refresh onCreate, perhaps you may want to add the code you want reloaded into onResume.
This workaround may work if you want to keep your code in onCreate.
finish();
startActivity(getIntent());
If all you want to do is refresh the content, you should override the onResume method, and add in the code to perform the refresh in this method. To do this, use the following code within the activity that you want to perform the refresh, (ie, not the same activity that you are calling startActivity() from):
#Override
protected void onResume(){
super.onResume();
//add your code to refresh the content
}
Tip: If you are using Android Studio, press Alt+Insert (while you have the Java file open), then click Override Methods, find onResume, and it should provide you with a basic template for the method.
The diagram I added shows the order that the methods are run (this is known as the Activity Lifecycle). onCreate() is run whenever an Activity is first created, followed by onStart(), followed by onResume(). As you can see, when a user returns to an Activity, onCreate() is not run again. Instead, onResume() is the first method that is called. Therefore, by putting your code into the onResume() method, it will be run when the user returns to the activity. (Unlike onCreate(), which will not be run again).
Extra info: Since you will be initially setting the data in onCreate() and then refreshing it within onResume(), you might want to consider moving all of your code used to initially set the data to onResume() as well. This will prevent redundancy.
Edit: Based on your following comment, I can give the following solution:
I'm wanting to properly refresh the page, e.g. if there is a variable count initialised at 0. And though running the activity it's has became equal to 300. When the activity is called (intent) then refreshed, count will once again be equal to it's initial value. Do you know how to do this?
Without your current activity's code, there is not much to work with, but here is some pseudo-code as to how I would accomplish your problem:
public class MyActivity extends Activity {
TextView numberTextView;
int numberToDisplay;
#Override
protected void onCreate(Bundle bundle){
super.onCreate(bundle);
setContentView(myContent);
numberTextView = (TextView) findViewById(R.id.numberTextView);
numberTextView.setText(numberToDisplay+"")//converts the integer to a string
}
#Override
protected void onResume(){
super.onResume();
numberToDisplay = 0;
numberTextView.setText(numberToDisplay+"");
}
}
In MainActivity starts another activity via startActivityForResult
Intent intent = new Intent(MainActivity.this, AutorisationForm.class);
intent.putExtra("req", 1);
startActivityForResult(intent, 0);
Method onCreate executes successful, activity displays on the screen and then app crash. Eclipse returns "source not found" error.
How I can solve this problem?
P.S. All activities declared in manifest.
P.P.S All worked successfully before I add a lot of logic in MainActivity. This code and second class hasn't changed.
Problem solved. Mistake was in onDestroy() method in MainActivity. In this method app try to save unloaded file. Theme closed.
Undo all changes before adding "a lot of logic in MainActivity."
Add changes one at a time and test in between until you discover problem.
Problem discovered!