Reputation: 1074
This is my second doubt of the day -.- I have one problem. I have a mainactivity that load a fragment and one service started in the mainactivity's oncreate.
In my fragment throught a listener inside the mainactivity i call one function of my service.
The problem is : when the fragment call the service function the service is not started.
How can I do ?
My service send a message to the mainactivity that call the correct method in the fragment.
I don't have an idea to solve this problem. . maybe i'm so too tired..
Maybe if I can know when fragment is completely load ! There is a way to know that ? Maybe if I call some function in fragment's onStart() ?
This is the mainactivity's code
public class ConnectionHandle extends Activity implements interfaceinthefragment
{
boolean mBound;
Service mService;
FragmentManager fragmentManager;
FragmentTransaction fragmentTransaction;
fragment fragment;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.connectionhandle);
//In this point we start the service
Intent intent = new Intent(this, servicetostart.class);
startService(intent);
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
//We load the correct fragment
fragmenttoload = new fragmenttoload();
fragmentManager = getFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragmenttoload);
fragmentTransaction.commit();
}
private ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service)
{
// TODO Auto-generated method stub
// Because we have bound to an explicit
// service that is running in our own process, we can
// cast its IBinder to a concrete class and directly access it.
mBound = true;
LocalBinder binder = (LocalBinder) service;
mService = binder.getService();
mService.setHandler(serviceHandler);
}
@Override
public void onServiceDisconnected(ComponentName name)
{
// TODO Auto-generated method stub
mBound = false;
}
};
private Handler serviceHandler = new Handler(Looper.myLooper())
{
@Override
public void handleMessage(Message msg)
{
someF(msg);
}
};
private void someF(Message msg)
{
//In this place I set something in the fragment depending of arrives messages from the service
}
//function in the interface inside fragment
@Override
public void startSearch()
{
if(mBound)
//function in the service
}
.
and now fragment code :
// onAttach
@Override
public void onAttach(Activity activity)
{
super.onAttach(activity);
try
{
discoverListener = (Listener) activity;
}
catch (ClassCastException e)
{
throw new ClassCastException(activity.toString()+" must implement Listener");
}
}
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
if (vwParent != null)
{
ViewGroup parent = (ViewGroup) vwParent.getParent();
if (parent != null)
parent.removeView(vwParent);
}
try
{
vwParent=inflater.inflate(R.layout.layoutx,container,false);
} catch (InflateException e) {
}
return vwParent;
}
@Override
public void onStart()
{
super.onStart();
firstScan=true;
deviceName = new ArrayList<xxx>();
adapter=new adapter.....
vwParent.findViewById(R.id.xx).setVisibility(View.INVISIBLE);
vwParent.findViewById(R.id.xx).setVisibility(View.VISIBLE);
vwParent.findViewById(R.id.xx).setVisibility(View.INVISIBLE);
//interface implemented by the mainactivity
**discoverListener.startSearch();**
ListView listView = (ListView) vwParent.findViewById(R.id.listView1);
listView.setAdapter(adapter);
listView.setOnItemClickListener(this);
}
the function in Bold (discoverListener.startSearch();) been called before the service real start and it doesn't works :/
I want handle the message from the service inside the mainactivity and not in each fragment. Did I wrong ?
EDIT : maybe in a better english What I would do is : from fragment call a function that is inside the service throught the function interfaceinthefragment.startSearch() implemented in the mainactivity. Then the service do some operation and gives the result through a message to mainactivity. This activity check the result message in function someF(msg) and do some function inside the fragment.
Upvotes: 0
Views: 770
Reputation: 1074
In the first post I have added the code. What I would do is : from fragment call a function that is inside the service through the function interfaceinthefragment.startSearch() that is implemented in the mainactivity. Then inside the service, do some operations and give the result through a message to the mainactivity. The MainActivity checks the result message with the function someF(msg), and depending to the result call the correct method inside the fragment and do some operations.
I hope can you give me an hand to understand what can I do :)
Upvotes: 0
Reputation: 16748
If you want to call a method inside a service, do so via calling the startService() with an intent ... then examine the intent in the service onStartCommand() and decide which method to call. You can call startService as often as you like. Here is an example.
I have this in an Application class
startService(new Intent(mContext, LocationMonitorService.class));
and this in my service:
public int onStartCommand(Intent intent, int flags, int startId) {
MyLog.p(this,"inside service making request for location updates");
mIntent = intent;
if (intent != null) {
if (intent.hasCategory("COM.GMAIL.NPNSTER.FIRST_PROJECT.MAP_FRAGMENT_RESUMED")) {
startRequestMarkerUpdates();
startLocationPushRequests();
} else if (intent.hasCategory("MAP_FRAGMENT_PAUSED")) {
endRequestMarkerUpdates();
endLocationPushRequests();
} else if (intent.hasCategory("LOCATION_UPDATE_REQUEST_RECEIVED")) {
deviceLocationClient.requestLLocationUpdates();
}
}
As far as making sure a Fagment is full loaded, override the fragments onResume method.
Upvotes: 1