Fragment in minimize Android Studio - android-studio

I have a Timer in a fragment and I want the timer to pause when the app is minimize(user clicks home or recent apps).
What should I do?
It seems that the fragment doesnt have onPause! The parent activity has it, which is useless because there I don't have access to the timer.
I also tried to make home button and set on click listener for it but I got null pointer exception error and when I used the code below
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId()==android.R.id.home)
{
Log.d("IT home","clicked");
}
return super.onOptionsItemSelected(item);
}
but it doesn't work. I didn't see anything it the Log cat.

according to the documentation , fragments have an onPause method. you can use it normally like you would do in an activity.
read more about it in the documentation.
I hope this helps you.

Related

Getting Change Event to Fire in SyncFusion's SFAutoComplete Control

I am trying to use the SFAutoComplete control from SyncFusion in a Xamarin iPad app. (only iPad).
I am not able to get any sort of change event to fire.
What I've tried:
If you download SyncFusion and install it, it comes with a "SampleBrowser" app that has samples for all the controls in the suite.
If you open that SampleBrowser in visual studio and open the AutoComplete_Tablet.cs file after line 97, I've added this code:
countryAutoComplete.ValueChanged += (sender, args) =>
{
suggestionModeLabel.Text = "IT WORKED!";
};
But it never fires.
I've tried to use several different events from the list of events this control has (partial list from screenshot):
None of them seem to fire (I haven't tried ALL of them).
What do I need to do to get one of these events to fire? What am I missing?
Thanks for using Syncfusion Controls.
Delegate property can be used to hook the SFAutoComplete's events as per in the following code example,
Declaration code for Delegate property
SFAutoComplete autocomplete = new SFAutoComplete();
autocomplete.Delegate = new SFAutoCompleteDelegate();
The way to hook the events in SFAutoComplete
public class SFAutoCompleteDelegate : AutoCompleteDelegate
{
public override void DidTextChange(SFAutoComplete SFAutoComplete, string value)
{
//It fired while changing the text in AutoComplete
}
public override void DidSelectionChange(SFAutoComplete SFAutoComplete, string value)
{
//It fired while changing the suggestion from suggestion box.
}
}
We have created a sample for achieving your requirement. Please download the same from the following link
Link:http://www.syncfusion.com/downloads/support/forum/125261/ze/testingAutoComplete_21799375630
Thanks & Regards,
Hemalatha M.R

how to drive DJ NativeSwing thread separately from my own thread

Nowadays, I am working on a java swing application using DJ NativeSwing as my embed browser to do something automatic work. The scenario of my application is that a user click start button and the embed browser auto click some position of the current page and then redirect to another one and then execute some other operations like click or something other. Now here is my solution. First, I will define a webbrowser class (extends JWebBrowser) and webbrowser listener class (implements WebBrowserListener), which represents a webbrowser and contains loadingProgressChanged, windowOpening and so on separately. Second, I define a thread class to do some logic computing and execute my browser operations as mentioned above by webbrowser.executeJavascript. At last, I add mouseListener for start and stop button to start or stop the task. When I open my application, the JFrame add the browser and its listener class. I click the start button, the browser works and will click the target position as I expected and then the page will redirect to another one. As we all know, js code can’t be executed until the page was loaded completely. So I set a global flag to check whether the page has loaded completely or not in loadingProgressChanged (code:if(e.getWebBrowser().getLoadingProgress() == 100)globalflag = true;) within webbrowser listener class. And in the thread class, I use code( while(globalflag == false){Thread.sleep(500);}) after first click to detect if the current page was loaded completely. However, when browser first click the target position and the page redirects successfully, I find that the current page has changed but blocked. After some debugging, I find it. In my thread class, browser will execute js code by webbrowser.executeJavascript("document.getElementById(‘target’).click();") to click the target position and then java code (while(globalflag == false){Thread.sleep(500);}) to detect if the current page was loaded completely and then execute some other java code. I find that the globalflag will never change and the current page’s loadingProgressChanged listener will never work because the java code (while(globalflag == false)). Because after I remove all java code after the first webbrowser.executeJavascript("document.getElementById(‘target’).click();"), the current page’s loadingProgressChanged listener works and the page was not blocked. With the DJ NativeSwing demo, I could execute my js in the loadingProgressChanged. However, I want to do a series of operations with my browser and also want to stop the task whenever need. So, I prefer to my solution for my demand rather than the provided one by demo. So, I wonder that after webbrowser.executeJavascript the DJ NativeSwing thread will wait my thread? And, in my software architecture, does anyone could have any suggestions? Thanks a lot. Any suggestion is appreciated!
PS.my application works fine with jdic-0.9.5, but it supports IE7 only.
I paste my code here to demonstrate my problem:
After I click the start button in JFrame, I will new a thread as follow
public class MyVisit implements Runnable{
private void doClick1(){
webbrowser.executeJavascript("document.getElementById('n').value='test'");
webbrowser.executeJavascript("document.getElementById('f').submit()");
}
public void run() {
globalFlag=false;
webbrowser.executeJavascript("document.getElementById(‘t’).click();") ;
while(!globalFlag){
Thread.sleep(500);
}
this.doClick1();
}
}
listener:
public class MyListener implements WebBrowserListener {
…
public void loadingProgressChanged(WebBrowserEvent e) {
if (e.getWebBrowser().getLoadingProgress() == 100) {
globalFlag=true;
}
}
}
DJ Native Swing enforces the Swing approach to threading: manipulate the components in the AWT Event Dispatch thread (a.k.a. UI thread). But also, do not block that thread, because listeners are triggered in that thread too.
So, you should not have a "while" loop in the UI thread, but should instead spawn a new thread. I hope your global variable is volatile, or AtomicBoolean or all accesses to it protected by synchronized block, otherwise you might have interesting threading side effects.

requestCastDeviceForRoute in onRouteAdded

I'm developing app for Chromecast. I'v tried Android Sample from https://github.com/googlecast/cast-android-sample, and he works fine - I may click on Cast icon, select device and start streaming. So, MediaRouter.Callback looks like this:
private class MyMediaRouterCallback extends MediaRouter.Callback {
#Override
public void onRouteSelected(MediaRouter router, RouteInfo route) {
MediaRouteHelper.requestCastDeviceForRoute(route);
}
}
And it works. But I want to start streaming on my device without stream button. So I'v changed this callback to:
private class MyMediaRouterCallback extends MediaRouter.Callback {
#Override
public void onRouteAdded (MediaRouter router, RouteInfo route)
MediaRouteHelper.requestCastDeviceForRoute(route);
}
}
But this doesn't work. This callback fires, requestCastDeviceForRoute returns true with required device (I see it by route.getName()), but onDeviceAvailable from CastSampleActivity never called. I'v tried delayed call of requestCastDeviceForRoute using Handler.postDelayed, and still nothing.
So, how to use requestCastDeviceForRoute properly to stream on Chromecast device without Media Button, right after he will be detected in MediaRouter.Callback?
Try calling
mediaRouter.selectRoute(route);
instead of using the MediaRouterHelper. This has worked for me when calling from within onRouteAdded()
Just to make sure I understand your question, are you trying to start streaming automatically as soon as the system "discovers" a cast device, without user clicking on the cast icon and selecting the device? Or are you trying to start streaming of the media immediately after user clicks on the cast icon and selects a target (i.e. removing the need to click on the Play button)?
The recommended flow is to let user choose the device manually, so what you are trying to do breaks the flow completely. What would user expect to happen if there is more than one chromecast device on the network? So it is strongly recommended not to go down that route.

BluetoothChat synchronized onResume Activity lifecycle method, why?

I'm studying right now the bluetooth Android API, and I ran into the BluetoothChat example.
http://developer.android.com/resources/samples/BluetoothChat/index.html
It contains many errors, first of all the simple fact that it uses API 11 but manifest does not force this minimum API.
Other interesting thing is the use of synchronized keyword on Activity lifecycle methods, like on onResume:
#Override
public synchronized void onResume() {
super.onResume();
if(D) Log.e(TAG, "+ ON RESUME +");
// Performing this check in onResume() covers the case in which BT was
// not enabled during onStart(), so we were paused to enable it...
// onResume() will be called when ACTION_REQUEST_ENABLE activity returns.
if (mChatService != null) {
// Only if the state is STATE_NONE, do we know that we haven't started already
if (mChatService.getState() == BluetoothChatService.STATE_NONE) {
// Start the Bluetooth chat services
mChatService.start();
}
}
}
Why this keyword is used there? Is there any reasonable explanation, or simply the one who wrote the code didn't know that onResume will be called always by the same thread? Or I miss something?
Thank you in advance!
This seems to be a pretty old question, but here's what I think may be going on:
My guess is that it wants to be careful about when "dialogs" return. The BluetoothChat example uses dialogs (as well as an overlay dialog-like activity) for enabling Bluetooth, enabling discovery, and initiating pairing/connections.
I don't know this for sure but I suspect there was a bug where different threads were returning to the main Activity and caused confusion as to how to handle onResume.
What they probably should have done is synchronize a block on an object and used flags to determine the state. That way the intention, state and functionality are more clear -- and the app knows what it should do in onResume;
something like this maybe:
//class fields
private Object myLockObj = new Object();
private boolean isPausedForPairing = false;
public void onResume()
{
super.onResume();
synchronized (myLockObj)
{
if (isPausedForPairing)
{
//handle a "pairing" onResume
}
}
}
However, due to it being an example app, they may have decided to go with something more simple. Example apps don't always follow convention because the idea is to demonstrate the particular code needed for the example. Sometimes following convention might add a lot of "distracting" code. Whether or not you agree with that is up to you though.

How to redraw a screen in Android

I want to know how I can for a screen to redraw it's current content?
What I have at the moment is the following: I have an activity that checks if a flag is set to true or false, if the flag is true it needs to load one xml file as it's layout, if the flag is false it needs to load a different one. This code works.
The problem I have is that the screen does not redraw itself unless I change the orientation of it, aka I flip the phone. I need the redraw to happen the moment the state of the flag changes. I had this working with the onResume(), but the problem with that was that the layout went back to the default state and did not save any input or button changes that had been made, this happened when I switched from one tab to the next.
So I guess what I am asking is something along the lines of how I can make changes to a layout file and keep them when switching between tabs in my application, so that when I go back to that tab, all the changes are still there?
Why don't you you an AsyncTask waiting in background for your flag to become true (or false idk), then onPostExecute you can modify your UI as you wish. Switching between tabs shouldn't make your Activity change. Hope it helps.
private class WaitMyFlag extends AsyncTask<Void,Void,Void>{
#Override
protected Void doInBackground(Void... params) {
while(!youflag){
try {
Thread.sleep(100);
} catch (Exception e){
Log.e(TAG, "Couldn't sleep !");
}
}
}
#Override
protected Void onPostExecute(Void params) {
//UI Modifications
}
}

Resources