Send data from Settings Activity to Watch Face - java

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.

Related

How to prevent activity from reloading itself when coming back after visiting other activities?

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.

How to run an activity once when you first open Android app

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

Strange issue with intent startActivity causing my scanner to not work properly

So, after I finish my scanner activity with
btn_take_photo.setOnClickListener(new FloatingActionButton.OnClickListener() {
#Override
public void onClick(View view) {
String carde = cardnumberbox.getText().toString().trim();
if (carde.matches("")) {
Toast.makeText(getApplicationContext(), getString(R.string.Skan_Udfyld_Kort_Nummer), Toast.LENGTH_SHORT).show();
cardnumberbox.requestFocus();
return;
}
Intent i = new Intent(ScanActivity.this, CameraActivity.class);
i.putExtra("EXTRA_SESSION_ID", carde);
startActivity(i);
}
});
to go to my cam activity so I can take some pictures and go back with
public void btn_aprove2(View view) {
Intent i = new Intent(CameraActivity.this, ScanActivity.class);
String counts = count.getText().toString().trim();
i.putExtra("EXTRA_SESSION_IDs", counts);
String carde = cardnumberbox2.getText().toString().trim();
i.putExtra("EXTRA_SESSION_ID", carde);
startActivity(i);
finish();
to the scanneractivity again. My scanner does not work properly
but if I then press the back button it does go back to the scanneractivity again instead of my menu so it seems like the scanneractivity is running twice and only 1 of them are functional but is here where it confuses me
cause if do not press the btn_aprove2 button and just use the back button instead
i gets the exact same issue but here my scanneractivity is not runned twice as when i press the back button it just takes me back to the menu
a video of the issue
by removing my screen orientation from the manifest (so i can rotate it)
my scanner do now work but only if i first rotate to landscape and rotate it back to potrait
and i see in the log is that it is only calling the oncreate when rotating and only on resume and pause on the button's(startactivity/finnish)
I am totally lost on how to get this to work.
on github with api demo and documentation in the wiki and with thoose classes that are being used
If you just jump to the Camera activity to get some data, I'd recommend you to start the activity for a result (startActivityForResult) without finishing the Scanner activity at all. This would give you a proper working back stack (using back button to go back from Camera to Scanner).
Besides that why are you using i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);? You are starting a new activity and finishing the old one. I don't see why this flag is really needed. What is your android:launchMode in your manifest and are you sure you know what FLAG_ACTIVITY_NEW_TASK is doing and it is what you want?
Anyways from what you told us it looks like your example really should utilize startActivityForResult() without calling finish():
Press menu button on some activity
Start camera and do something
Press some button to start Scanner
Scan something and finish the scanner with the result (or cancel the scanner by clicking back)
Retrieve the result of the scanner in the camera and do something with it or continue with the previous workflow when scanner was cancelled
Once you are finished with your workflow, finish the camera so you end up in your activity where you started the camera

How to navigate back to main activity without a button

I'm accustomed to using an intent to launch a new activity when a button is pressed.
But the app that I'm making uses a list view, which means I want to be able to go back from my searching activity to the main activity by using the phone's back button.
I was reading and experimenting with different types of android methods, this one in particular which seemed simple but doesn't work, or perhaps I'm doing something completely different.
public void onBackPressed()
{
Intent setIntent = new Intent(this,MainActivity.class);
startActivity(setIntent);
finish();
}
Doesn't Android's onBackPressed method respond to any android phone's back button?
Thank you for your help.
You forgot to call super.onBackPressed() inside onBackPressed() method, you dont need to start a new intent to go back.
In your search activity override the onBackPressed() method and call it from wherever you want in the activity.
it should be like this.
#Override
public void onBackPressed(){
super.onBackPressed();
}

how to make the Fragment/Activity not go to the backgoround

Normally In an Fragment, implicit intent can be used to start a brower component,if there are several brower component, the Android system will show an selection dialog ,the dialog just overlay the Fragment which send the intent.
but in my situation when selection dialog is shown ,the Fragment go to the background(disappeared) ,and the home screen can be seen.
does anyone encounter this problem ,how to make the Fragment not go to the backgoround.
in onPause method it just look like below in my fragment source
#Override
public void onPause() {
mLog.printMethodLifeCycle("onPause");
super.onPause();
}
and in activity I did not overide the onPause method
AndroidManifest.xml is too long ,can you tell me which part should I post or notice

Categories

Resources