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 6906971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:22:40+00:00 2026-05-27T08:22:40+00:00

I am trying to send the events from one Java class to Activity. Scenario

  • 0

I am trying to send the events from one Java class to Activity.

Scenario is, Will be having some data in the native, native will call the callback function which is in java code, This class processes data, after the processing i need to update the UI. I want to update the UI at one place in the Activity handler. (Dont want to use runOnUiThread() everywhere).

I was not able to send the events properly with the below approaches.
1st Approach:
1) Define functions for posting messages in to the queue and call these functions.
2) To call the above mentioned functions (point 1) we need context, if i maintain the static variable for maintaining the context and returning it, if the activity is created twice we wont able to get the write context for the first activity.

public class Activity1 {
    protected static Context myContext = null;
    protected Handler myHandler = null;

    @override 
    public void onCreate() {
        myContext = this;
        myHandler = new Handler();
    }

    public static Context getMyContext() {
        return myContext;
    }

    public void postEvent1() {
        myHandler.sendMessage();
    }
}

2nd Approach:
1) Making the handler as a static variable and returning this with the help of static function. – Not a good design to expose the internal variables.
2) Cons will be like above, when a second activity is created.

public class Activity1 {
    protected static Handler myHandler = null;

    @override 
    public void onCreate() {
        myHandler = new Handler();
    }

    public static Context getMyHandler() {
        return myHandler;
    }
}

Is it possible to get the activity context without using the static variables and static functions?

Please share the knowledge if anyone knows. 🙂

Thanks & Regards,
SSuman185

  • 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-05-27T08:22:40+00:00Added an answer on May 27, 2026 at 8:22 am

    I used a container class HashMap for storing the contexts with the key.

    I used the name of the class as the key.

    When the second activity is trying to register with class containing hashmap, it will reply with context of the already stored activity (null if not).

    So like this I am able to store the contexts’ of the classes and avoid loosing of the first activity context if I am creating the second one.

    Please add if any one gets better solution.

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

Sidebar

Related Questions

I'm trying to send data from an overlay to other activity with this class
i'm trying to send a byte[] from one activity to another. in the recieving
Trying to send some email in my C# app. I am behind a proxy
I'm trying to send an email in Java but when I read the body
I am trying to send an email from a site I am building, but
I'm trying to create a Qt main windows from Java using JNI directly and
I'm trying to design a system which reports activity events to a database via
I am creating a class that implements the SipListener interface to receive events from
I'm trying to take a variable from a php page and send it to
I am trying to send touch events using batch files (.bat) and the adb

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.