How to stay connected to an Android service between multiple activities?
I have multiple activities and one service. In MainActivity
I successfully connect to service (using a class that implements ServiceConnection
+ bindService()
+ startService()
) but when I try to apply same method in other activity I see in LogCat a error:
01-15 22:29:37.438: ERROR/ActivityThread(12206): android.app.ServiceConnectionLeaked:
Activity com.app.liviu.simpleMusicPlayer.PlaylistActivity has leaked ServiceConnection
com.app.liviu.simpleMusicPlayer.PlaylistActivity$CounterServiceConnection@43713c90
that was originally bound here.
Is it possible to connect to a service in an other way: something like to make static my CounterServiceConnection
object in MainActivity
and use it in the second one?
Asked by: Tess758 | Posted: 24-01-2022
Answer 1
The error message is fairly self-explanatory: you are leaking a ServiceConnection
, by which Android means you are calling bindService()
but are not calling unbindService()
, and the activity that called bindService()
was destroyed. Make sure you call unbindService()
in onDestroy()
, if not sooner.
something like to make static my CounterServiceConnection object in MainActivity and use it in the seconde one?
No.
Answered by: Rebecca474 | Posted: 25-02-2022Answer 2
You can bind it from a separate thread as long as you install a looper in that thread
Answered by: Audrey485 | Posted: 25-02-2022Similar questions
java - Binding few activities to one service - class which connected treated like service - can't destroy activity
I will start from my approach I want to have one service which is running even if none of application screen is visible for user. This service will scan for beacons.
Every screen of application needs to get access to method of service so I used binding to service here.
I designed serviceconnector class which will be connecting Activities with service, this class look like this.
import android.content.C...
java - Playing BG Music Across Activities in Android
Hello! First time to ask a question here at stackoverflow. Exciting! Haha.
We're developing an Android game and we play some background music for our intro (we have an Intro Activity) but we want it to continue playing to the next Activity, and perhaps be able to stop or play the music again from anywhere within the application.
What we're doing at the moment is play the bgm using MediaPlayer at ou...
Android: What is better - multiple activities or switching views manually?
I have developed some apps for Android, and this questions stays always:
How should I structure my UI? Should I launch activity after activity and leave the phone to make the "back" button, or should I choose more optimized, but more complex to implement, way with switching manually Views and then manually doing the "Back" button functionality?
What do you think (or know) is the better practice?
android - Is it better to create new Activities or just create a different Layout and replace the existing layout?
Since I am new to Android, I am now thinking on what is the correct way of doing things.
As it stands, the application I'm writing has 4 different screens:
Screen 1 - list of nodes (main screen)
Screen 2 - options menu, tableLayout with buttons
Screen 3 - navigation
Screen 4 - text details on version etc
These screens can be navigated to/from using a "header" ...
android - freeze on sending certain bitmaps to activities
Basically I receive the Image's URI from the Gallery, then created a Bitmap and want to send to another activity for displaying:
Uri imageUri = intent.getData();
mBitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), imageUri);
Intent intent = new Intent(TakePictureActivity.this, PreviewActivity.class);
intent.putExtra(EXTRA_BITMAP_DATA, mBitmap);
startActivityForResult(intent, REQUEST_PREVIEW);
...
problem in passing data in android Activities?
can any one guide me what mistake am i doing in this code??? it not seems to be working..
i have two activies
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent intent = new Intent(DataPassing.this, DataPassing2.class);
Bundle b = new Bundle();
b.putInt("key", 1123);
intent.putExtras(b);
...
Android : launching diff activities under TabWidget
I am trying to launch activities under each tab.
I have tried with following code
public class Tab_Proj1 extends TabActivity {
TabHost mTabHost ;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Context context = getApplicationContext();
//mTabHost = (TabHost) this.findViewById(R.id.);
mTabHost = getTabHost();
mTabHost.setup();...
Android app with multiple activities
I have a very simple game that consists of only one activity, and I want to add a title screen.
If the title screen is another activity, what changes do I need to make to my manifest file to make the title screen open first?
The gameplay activity is called Leeder, and the title screen activity is called LeederTitleScreen
here is my current manifest file.
<?xml version="1.0" encodi...
xml - Android - Should I use multiple activities or multiple content views
i'm new with android.
I'm working on an application using xml layouts.
I wish to know which is better:
1. Use few activities and change its contentview
2. Use an activity for each 'view' needed
If both works, in which case which option would be better?
thx a lot
android - Sending data between activities within my app?
I have a TabActivity, and the tabs point to sub activities. Is there a way I can send a 'message' to those child activities? I just want to pass a string across, not sure if this is possible.
I have some data being fetched by the parent TabActivity, and the child tabs can't do anything useful until the parent is done fetching. When fetching is complete, I'd like to pass that data to the child activities so they ca...
java - When to use new layouts and when to use new activities?
I'm making a game in Android and I'm trying to add a set of menu screens. Each screen takes up the whole display and has various transitions available to other screens. As a rough summary, the menu screens are:
Start screen
Difficult select screen
Game screen.
Pause screen.
Game over screen.
And there are several different ways you can transition between scre...
Still can't find your answer? Check out these communities...
Android Google Support | Android Community | Android Community (Facebook) | Dev.io Android