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
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 created a startup activity from where I am calling another activity which has a view pager and shows some introductory pages.
This app was taking time to load so I thought to display a progress dialog till the activity loads, but that progress dialog also appears few seconds later.
startup activity:
public class StartUpActivity extends AppCompatActivity {
boolean isUserFirstTime, login;
public static String PREF_USER_FIRST_TIME;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
isUserFirstTime = Boolean.valueOf(Utils.readSharedSetting(StartUpActivity.this, PREF_USER_FIRST_TIME, "true"));
Intent introIntent = new Intent(StartUpActivity.this, SlidingActivity.class);
introIntent.putExtra(PREF_USER_FIRST_TIME, isUserFirstTime);
ProgressDialog dialog = new ProgressDialog(StartUpActivity.this);
dialog.setMessage("Welcome to Mea Vita, please wait till the app loads.");
dialog.setCancelable(false);
dialog.setInverseBackgroundForced(false);
dialog.show();
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
//Here you can send the extras.
startActivity(new Intent(StartUpActivity.this,SlidingActivity.class));
// close this activity
finish();
}
}, 4000);
}
}
This doesn't happen every time,only sometimes. What can be the possible reason for this? how can I stop this?
Any solution? Thank you..
There is a strange issue with newly released Android Studio 2.0 (same issue in 2.1) first time of launching application take longer than usual (e.g. 2, 3 seconds or sometimes screen blinks or goes black) this issue happens only in debug mode and not effect your released APK.
A temporary solution to fix this is disabling instant run:
Settings → Build, Execution, Deployment → Instant Run and uncheck Enable Instant Run
First of all, make as rule to make all data loading in async tasks, you must check activity that you want to start where you load data.
The problem is in your second activity.
oncreate method should be used only to make findviews or start async tasks, don't load any in oncreate or in onstart or in onresume.
Probably you are loading high res images in sliding layout or you loading data in it.
There is another way, load all data in async task on first activity, then with ready data start second activity with already data loaded.
There are a few things that can load slowly.
Android need to read your code from storage and load the classes into ram.
I assume Utils.readSharedSetting(StartUpActivity.this, PREF_USER_FIRST_TIME, "true") reads from preferences. That's a file that you're reading from synchronously.
Actually launching the dialog takes a very small amount of time.
I'd suggest showing your loading inside the activity itself to minimize the work needed to render it.
Also, you can store PREF_USER_FIRST_TIME as a boolean instead of a String.
I just developed a simple Android Wear Watch Face to start learning something about Android Wear.
App: https://play.google.com/store/apps/details?id=com.gerardcuadras.minimalflat (So simple)
The watch face change his Background color when the user taps the screen. Now, I want to add an Activity that show's a list of colors and let the user select one instead of tapping the screen.
I added this lines to my manifest that adds the config toggle to the watch face.
<meta-data
android:name="com.google.android.wearable.watchface.wearableConfigurationAction"
android:value="com.gerardcuadras.minimalflat.wearable.watchface.CONFIG_DIGITAL" />
After that I created an Activity that handles the button click:
public void broadcastIntent(View view){
Log.d(TAG, "broadcastIntent: Pressed button");
Intent intent = new Intent();
intent.setAction("com.gerardcuadras.minimalflat.CUSTOM_INTENT");
intent.putExtra("extra", "extra data");
sendBroadcast(intent);
}
Then on MyWatchFace.java (extends CanvasWatchFaceService) I have this that it supposes to handle the broadcast:
public class MyReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "onReceive: WORKS!");
}
}
When I run the emulator and click at the button, the only thing that I see on the console is:
"broadcastIntent: Pressed button"
So the receiver (maybe the sender) isn't working because it's not logging the onReceive() log.
I'm really new at Android. I'm sure I'm not doing this well or I'm missing something.
I will appreciate any help or any documentation to continue learning step by step.
Thanks
This might be a better fit for using SharedPreferences (http://developer.android.com/reference/android/content/SharedPreferences.html)
With SharedPreferences the selection will be persisted even if the user changes to a different watch face and back.
You can also use a registered Preference Change Listener to detect the changes made in your configuration activity.
http://developer.android.com/reference/android/content/SharedPreferences.OnSharedPreferenceChangeListener.html
You can use the WearPreferenceActivity library for your PreferencesActivity, which changes the SharedPreferences android:key="pref_key". Your watch face reads preferences via mSharedPref.getBoolean("pref_key", false);.
One easy way to detect preference changes is, to listen to the onVisibilityChanged event, which will be called after the PreferencesActivity gets closed.
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));
}
I m a newbie an trying to learn Java/Android-programming.
I m doing an app for Android in Eclipse and created some buttons.
I have a back and a cancel button.
Example:
I have a EditText there you can write in your name. If you write yourname and press the backbutton, then u will go back to the previous Activity, but if you go to the same Activity, then you will still see the name that you wrote in the EditText.
But if you press the cancelbutton, you will go back to the previous Activity, but when you come back, yourname will be empty. I will "kill" or "stop" the Activity.
This is the code I use for the Backbutton, what would you use for the Cancel Button?
Thank YOU.
public void onClick(View v) {
switch(v.getId()){
case R.id.buttonBack:
Intent intent = new Intent (AllActivity.this, MenuActivity.class);
startActivity(intent);
break;
For the cancel button you can use the below method, this will kill the activity.
finish()
so in your code it will look something like this:
public void onClick(View v) {
switch(v.getId()){
case R.id.cancel:
finish();
break;
There was little difference in this as per requirement of process or application flow. For cancel and back as work are same for example if you open any dialog and provide cancel button will close/dismiss your dialog same way the back button do this. While for implementing with the Activity you if you implement for closing current activity you can just finish with both option by just calling finish() method. As back button was normally work for finish you current activity and back.
Another way to do this that you may be interested in is to wipe out the content of the EditText yourself.
You would need to have in your xml file an id defined for the EditText so that you could access it programatically.
<EditText
layout stuff here:
android:layout_width="fill_parent"
...
and then the id attribute
android:id="#+id/edit_text_id"
>
then in your code you would put the following in your class (not inside any method):
EditText anEditText;
then in your onCreate(), after the inflation of the layout (if it comes beforehand it will cause the app to crash):
anEditText = (EditText) findViewById(R.id.edit_text_id);
the name edit_text_id is not significant, but it is what we used in the layout file
next add to the onClick method for cancel (after the case statement):
//this wipes the text from the textbox
anEditText.setText("");
// add the rest of the back button code after this and your good!
Best of luck! Remember that we were all newbies once. If you want to be a good android programmer, I suggest that you get a strong background in Java first. This free book helped me very much!
Java Notes