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

  • SEARCH
  • Home
  • 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 8172027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T21:39:05+00:00 2026-06-06T21:39:05+00:00

Can i initialize object in my first activity and you it in all activity???

  • 0

Can i initialize object in my first activity and you it in all activity???

public class Calc{
    int x;
    int y;
    public Calc(int x, int y) {
       this.x = x;
       this.y = y;
    }
    public int sum() {
        return x + y;
    }
}
public class MainActivity extends Activity {

private int progressStatus = 0;
private Handler handler = new Handler();
private ProgressBar loading;
private static int progress;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


    Calc c = new Calc(3, 4);
}
}


public class PreviewActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.preview);

            TextView txt = (TextView) findViewById(R.id.txt);
            txt.setText(Integer.toString(c.sum));
}
}

How can i Initialize Calc object in MainActivity and use it PreviewActivity.

How can i pass it to another activity or how can i make it share with other activity

  • 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-06T21:39:07+00:00Added an answer on June 6, 2026 at 9:39 pm

    Use Application class by extending it and writing your custom Application class, and keep your objects in this class that you need in your cross Activities

    class MyApplication extends Application{
        Object a;
    
        public void setA(Object a){
             this.a = a;
        }
    
        public Object getA(){
             return a;
        }
    
    }
    

    Now lets suppose in your A activity you create object of class Object and want to use it in your B Activity.

    do it this way,

    class ActivityA extends Activity(){
    
    ...
    // some where in activity, set your object this way.
         Object aObj = new Object();
         ((MyApplication)getApplication()).setA(aObj);
    
    
    ...
    
    }
    
    class ActivityB extends Activity(){
    
    ...
    // some where in activity, get your object this way.
         Object aObj = ((MyApplication)getApplication()).getA( );
    
    
    ...
    
    }
    

    You need to tell your androidManifest.xml about your extended Application class.

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

Sidebar

Related Questions

I would like to know: how can i initialize class object with parameters? I've
Can I use form_for orbinary object? Controller: class AgendaState attr_accessor :base_date def initialize(date) @base_date
Can we initialize Python objects with statement like this: a = b = c
Using the C# object initializer syntax I can instantiate an anonymous object like this:
I love the Hash implementation of Ruby where you can initialize the Hash object
I have the following situation: public abstract class A { private Object superMember; public
First of all, sorry to post a question like this when so many other
Using C# 3.0, we can initialize objects without their constructors for syntactical reasons. Such
There are several different ways I can initialize complex objects (with injected dependencies and
How can I use an object initializer with an explicit interface implementation in C#?

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.