Just display notification and background app stay onResume - android

I have scenario where I want to develop an app on TV, in that if user is watching some videos on Youtube and if I want to load my app, then it will launch in foreground and I will display some information with transparent background and behind app will continue to work, like if behind app is Youtube then it should play as it is without pause.
Please help me if it is possible? if yes then how can I achieve that?

In Manifest Define Activity theme as transulent :
<activity android:name=".YourActivity" android:theme="#android:style/Theme.Translucent.NoTitleBar" />
For Transulent Activity life cycle differs from Non-transulent Activity :
For Non-transulent Activity-A if any other Activity-B comes on top of
it then Activity-A will be in onStop state.
Activity-A if any other Activity-B comes on top of it then Activity-A
will be in onPause stateBut where as For Transulent
Then do this :
#Override
public void onPause() {
super.onPause();
if (mVideoView.isPlaying()) {
// Argument equals true to notify the system that the activity
// wishes to be visible behind other translucent activities
if (! requestVisibleBehind(true)) {
// App-specific method to stop playback and release resources
// because call to requestVisibleBehind(true) failed
stopPlayback();
}
} else {
// Argument equals false because the activity is not playing
requestVisibleBehind(false);
}
}
I did implementation it worked go through this it will help u for sure
:
https://developer.android.com/training/tv/playback/options.html
If you requirement is related to Picture in Picture try this :
https://developer.android.com/guide/topics/ui/picture-in-picture.html

Related

Destroy / Pause Android Service (music) on HomeButton pressed

I tried to add some Music in the Background of my Main Menu. I finally achieved it by using extends Service. Now i have another problem:
If the Home Button is pressed, the Service still runs and plays music, it only finishes, if i destroy it. I tried using:
#Override
protected void onPause(){
super.onPause();
service.setAction("model.BackgroundSound");
stopService(service);
}
which works fine, but if i switch from my Main Menu to my Character Selection Activity, it will pause too. But i would like to keep the music going in all Activities.
Is there a way to detect the difference of the App being "on pause" or just the activity?
You can use fragments instead of Activities and it will call your Pause() just for the Single Activity.
If you are using API level 14 and higher you can use registerActivityLifecycleCallbacks() on the Application. You can check and example usage in this SO answer.
This way you can check if any of your app's Activities is visible.

Restart app from launch screen instead of resuming

I have an application with basically a list of items and a detail screen for each items.
When initially started, we show the list of items. If the user switches to another app when viewing a details screen, when he comes back to the app, the details screen is shown.
All that is the standard, and working well. However, my client needs the user to come back to the list screen instead of the details screen each time the app is resumed.
My first idea would be to remember the time at which the details activity got paused, and when started, if the time is greater than X seconds, finish and launch list activity instead of resuming.
Any more reliable way to do that?
PS: I know we should not do that, I already explained that to my client, decision is not mine.
Use SharedPreference to save the time of your paused detail activity in onPause and when it resume check the saved time with current time whether it has passed your threshold if it is passed then close it otherwise remain it opened.
Implement this solution and it definitely helps.
Basically, the app is not actually restarting completely, but your launch Activity is being started and added to the top of the Activity stack when the app is being resumed by the launcher. You can confirm this is the case by clicking the back button when you resume the app and are shown the launch Activity. You should then be brought to the Activity that you expected to be shown when you resumed the app.
The workaround I chose to implement to resolve this issue is to check for the Intent.CATEGORY_LAUNCHER category and Intent.ACTION_MAIN action in the intent that starts the initial Activity. If those two flags are present and the Activity is not at the root of the task (meaning the app was already running), then I call finish() on the initial Activity. That exact solution may not work for you, but something similar should.
Here is what I do in onCreate() of the initial/launch Activity:
if (!isTaskRoot()
&& getIntent().hasCategory(Intent.CATEGORY_LAUNCHER)
&& getIntent().getAction() != null
&& getIntent().getAction().equals(Intent.ACTION_MAIN)) {
finish();
return;
}
for more details on isTaskRoot()method reference.
You have to provide the following onPause() method to all the activity classes except your list_item activity(Initial Activity).
#Override
protected void onPause() {
super.onPause();
Intent i = new Intent(getApplicationContext(), list_item_activity.class);
startActivity(i);
}
I might understand your problem incorrectly but why you do all the timing stuff? I mean, assuming you've fragments for your list and detail views, just put a a flag to monitor your activity has stopped and listen to catch your activity resume ( via onResume or onWindowFocusChanged ). If it's stopped and resumed then transition to list fragment if it's not already visible.
You can you a broadcast receiver here.
And on activity OnResume method use a call to broadcast receiver and perform whatever you need like this.
#Override
protected void onResume() {
super.onResume();
sendBroadcast(new Intent("YourActionHere"));
}

App is not being restored when launched from Home

I am trying to control my stop/restart behavior according to the Android Guidelines but it's getting a bit tricky. I want my app to restore state after being minimized and tapping the launcher icon.
The current behaviour makes so I can HOME out of the app, and when the user comes back using the long press list everything behaves as expected. Now, trying to return tapping the app icon starts the default activity, it doesn't take onRestoreInstanceState into account, and while keeping the old task affinities open on their back stack.
The activities have onCreate, onResume, onSavedInstanceState overridden, calling their super, and are of type SingleInstance.
During one of my app development, I had the similar issue where app have single activity and need to save some state values (usually string, Boolean) onpause. I will give you idea how i handle it, and I hope it will useful to you also.
protected void onPause() {
super.onPause();
saveState();
}
protected void onResume() {
super.onResume();
retrieveState();
}
donot forget focus change ...
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if(hasFocus) {
retrieveState();
}
else { saveState(); }
}
Now the save and retrieve data
public void saveState()
{
SharedPreferences settings = getSharedPreferences(WEBSTATE, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString(CURRENT_URL_STATE, "some url");
editor.putInt(FILECOUNT, 10);
/* any other content... */
editor.commit();
}
public void retrieveState()
{
SharedPreferences settings = getSharedPreferences(WEBSTATE, 0);
String current_url = settings.getString(CURRENT_URL_STATE, "");
int count = settings.getInt((FILECOUNT, 0);
/*use these value as per requier.. */
}
The current behaviour makes so I can HOME out of the app, and when the
user comes back using the long press list everything behaves as
expected. Now, trying to return tapping the app icon starts the
default activity, it doesn't take onRestoreInstanceState into account,
and while keeping the old task affinities open on their back stack.
Let's say you got 2 activities: A and B. Activity A is your entry point, so when you tap on app icon (which in fact is incorrect statement as this is activity icon, NOT the app icon) you launch activity A. Now you got to activity B and press HOME. When you now open list of recents, you will see activity B, and will be able to get back to it. But when you tap your "app icon" on launcher screen, you will enter activity A. And this is normal and correct behavior. Also note savedInstanceState is not overall state of your application as you most likely think.
Also note, that your Activity class can be instantiated multiple times, which depending on your application architecture can, also lead to incorrect navigation behaviour. You can try to control this by using android:launchMode in your Manifest file (you will find android documentation here).
Finally, if possible, consider reworking your application to use Fragments instead of multiple activities - that would simplify controling application flow.
Not sure if this is what you want to do, But i had a similar problem where i wanted a single base activity to return to, And i couldn't get the desired result from SingleInstance, I defined a new Activity which was the main launched :
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
And from that activity i launched my desired Activity with the desired FLAG clear-top, and finished the main launched activity.

when touch back button on android how to quit the app by cocos2d-x

A game created by cocos2dx. In active scene, when I touch the back button on android, how can i quit it!
can give same example?
Version cocos2d-2.0-x-2.0.4 and cocos2d-x-2.1.4
in our Layer.h:
...
void keyBackClicked();
...
in our Layer.ccp:
Layer::init(){
...
this->setKeypadEnabled(true);
...
}
void Layer::keyBackClicked() {
CCDirector::sharedDirector()->end();
}
Now you are in a position if you click back then you are navigated to the previous screen where you started off your game application right? Then here is the solution: after navigating to the new intent, the previous screen gets in an inactive state in android activity lifecycle you can find that the previous screen goes to a invisible state. Now we can use onPause() method to close the hidden activity. In the same class add this code and your app should be closed when you press back button.
protected void onPause() {
super.onPause();
finish();
}
When you click any button and go to a new intent the application goes to the invisible state and the onPause() method is triggered automatically and it closes the same intent in background.
Override backKeyClicked() method in your layer.
Don't forget to add this->isKeypadEnabled(true) in init method of layer.
In you backKeyClicked method, you can switch it to previous scene or whatever you wanna do.

Android: Different start activity depending on user preference

My application starts with a welcome screen Activity, but that screen has an option to skip that screen altogether in future launches.
What's the proper Android way to do this? Initially, I just automatically detected the skipWelcome preference and switched to the 2nd activity from Welcome. But this had the effect of allowing the user to hit the back button to the welcome screen we promised never to show again.
Right now, in the Welcome activity, I read the preference and call finish() on the current activity:
SharedPreferences preferences = getPreferences(MODE_PRIVATE);
boolean skipWelcome = preferences.getBoolean("skipWelcome", false);
if (skipWelcome) {
this.finish();
}
And then I implement onDestroy to move on to the next Activity:
#Override
public void onDestroy() {
super.onDestroy();
startActivity(new Intent(Welcome.this, StartFoo.class));
}
But this makes for some weird visual transitions. I'm starting to think that I need a base Activity that pops open Welcome only if proper, and then goes to StartFoo.
I can't comment on Mayra's answer or I would (not enough rep), but that's the correct approach.
Hidden in the Android documentation is this important phrase for Activity.startActivityForResult(),
"As a special case, if you call
startActivityForResult() with a
requestCode >= 0 during the initial
onCreate(Bundle
savedInstanceState)/onResume() of your
activity, then your window will not be
displayed until a result is returned
back from the started activity. This
is to avoid visible flickering when
redirecting to another activity."
Another important note is that this call does not block and execution continues, so you need to stop execution of the onCreate by returning
if (skipWelcome) {
// Create intent
// Launch intent with startActivityForResult()
return;
}
The final piece is to call finish immediately in the welcome activity's onActivityResult as Mayra says.
There are a few solutions to this.
Did you try just launching the activity and finishing? I vauguely remember that working, but I could be wrong.
More correctly, in if(skipWelcome) you can start the new activity for result, then when onActivityResult is called, immidiately finish the welcome activity.
Or, you can have your launcher activity not have a view (don't set content), and launch either the welcome activity or StartFoo.

Categories

Resources