Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8384541
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:25:45+00:00 2026-06-09T17:25:45+00:00

I have a application where in the main activity the user selects a Bluetooth

  • 0

I have a application where in the main activity the user selects a Bluetooth device and the app connects to it. Then a new activity is launched where the user can select between different activities (there are different ways to interact with the Bluetooth device). Lastly the selected activity is launched and the user is happy.

My problem is that when the connection is lost I should finish the list of activities activity and interaction activity somehow… but how? I’ve heard of Intents to pass messages between activities and Broadcasts, even Helpers, but no examples of how to pass an information from a running thread to multiple other activities.

The different connection threads (which I borrowed from the BluetoothChat example) are in the Application class, so I can access the writing function from any activity. That’s also where I detect the lost connection.

Here’s the relevant code from my app:

the Application class:

public class BluetoothRemoteControlApp extends Application {
    public final int BT_CONNECTION_LOST = 1;

    // . . .

    private class ConnectedThread extends Thread {
        // . . .

        public void run() {
            byte[] buffer = new byte[1024];
            int bytes;

            while (true) {
                try {
                    bytes = mmInStream.read(buffer);
                    mHandler.obtainMessage(0, bytes, -1, buffer).sendToTarget();
                } catch (IOException e) {
                    e.printStackTrace();
                    // the lost connection is detected here
                    connectionLost();
                    break;
                }
            }
        }

        // . . .
    }

    private void connectionLost() {
        Log.e("BT", "Connection lost");
        // inform that connection was lost,
        // finish all activities and (re)start device select activity again
    }
}

The activity where the different activities (called “actions”) are proposed:

public class ActionListActivity extends ListActivity {
    ArrayList<Action> activityList = new ArrayList<Action>();

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // list of available activities, arguments: title, description and class name
        activityList.add(new Action("Accelerometer Control", "Control your robot by tilting the phone", "AccelerometerControl"));
        // . . .

        setListAdapter(new ActionListBaseAdapter(this, activityList));
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);

        String activity = activityList.get(position).getClassName();

        try {
            Class<?> activityClass = Class.forName("com.bluetooth.activities." + activity);
            Intent intent = new Intent(ActionListActivity.this, activityClass);
            startActivity(intent);
        }
        catch(ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    // add a method here to finish the activity when connection is lost
}

So let’s say I launch my AccelerometerControl activity, what to put in connectionLost() to send a message to ActionListActivity() and AccelerometerControl() so that they would finish?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T17:25:47+00:00Added an answer on June 9, 2026 at 5:25 pm

    use startActivityOnResult instead of startActivity and when you want to finish the activity use finishActivity (int requestCode) with the same requestcode used in startActivityOnResult

    Edit:
    Create an ArrayList of Handlers in Application and also a static BluetoothRemoteControlApp field in your Application class and instantiate this to the present Application class instance.

    In each Activity create a Handler. In Activity’s Onresume register this Handler with Application using static field of BluetoothRemoteControlApp.

    As soon as Connection is dropped, Iterate over all the handlers and sendmessage.
    In all Handler’s , handleMessage use finish().

    In Onpause remove this handler from Application

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my application, I have specified a second activity that can be launched from
I have an application about debts to user and user's debts. Main activity is
I have an android application which in its main activity, data are adapted from
I'm writing an android application using eclipse. I have a main activity called MenuActivity
I have an app with 3 activites. At my opening activity (main activity), I
I have an application that contains a main Activity, which has a very simple
I have an activity with 3 EditTexts and Button. User can fill two edittexts
The application I'm working on has an activity feed where each user can see
In my Android app, I have a main activity that serves as an entry
There are several packages in my application that user can select each one according

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.