I have a Main Activity and Child Activity.
MainActivity will intent the ChildActivity and call moveTaskToBack(true) at last to hide itself, but not finish().
On ChildActivity, i want to access the getContext(),getApplicationContext() method from MainActivity, so how can i do that?
Thanks you.
One way you can access using static variable, like this
In parent class declare
Public static String parentContext;
assign context in onCreate(...) method
parentContext = parentActivity.this;
or
parentContext = getApplicationContext();
Now in your ChildActivity
Context childContext = parentActivity.parentContext;
Related
I want to know how can I send the context of an activity from ClassA.java to MainAddingItems.java.
I can pass the MainActivity.class as the argument but I am not able to pass the context.
Here is some of the code of ClassA calling the constructor of MainAddingItems
new MainAddingItems(MainActivity.class,"MainActivity Clicked",R.id.activity_main_linearLayout,"Profile Acitvity")
It will work ..make sure that you declerad it as public and static like below
Public static Context context ;
In your main activity
You can use any where that context in whole project
You can do this. If your constructor is:
MainAddingItems(Context context) {...}
Then from Activity1 you can just do:
Context context = Activity1.this; // Or getApplicationContext() or View.getContext() or whatever context you want
MainAddingItems(context);
Make your MainAddingItem class's constructor as this,
class MainAddingItem {
Context context;
MainAddingItem (Context context, rest of the parameters){
this.context = context;
}
}
Use the above constructor and pass the activity's context in the constructor's parameter.
new MainAddingItem (this, rest of the parameters);
Actually you are passing the context here . In android context is the current state of application . As an example if you are in a activity, then the context is the activity class itself , same for service and any other component of android
Let me give another example ,
Just try to show a toast message in onCreate method of any activity , considering your activity name is SimpleActivity you may write something like this
Toast.makeText(this "Understating context",Toast.LENGTH_LONG).show();
Or you may write the following
Toast.makeText(SimpleActivity.this "Understating context",Toast.LENGTH_LONG).show();
The first parameter of makeText method is context , and it works if you simply pass the class .
This is how you will pass activity as an argument
public SimpleClass{
AppCompatActivity mActivity;
public SimpleClass(AppCompatActivity mActivity){
this.mActivity = mActivity;
}
}
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SimpleClass newSimpleClass = new SimpleClass(MainActivity.this);
}
}
Just make Context as static like in MainActivity.
Public static Context contextToUse;
You can use like below :
MainActivity.contextToUse
Please let me know ,if it is helpful to you
I am starting other activity using intent from MainActivity.
Here is my code:
Intent intent = new Intent(this, ProgramClass.class);
startActivity(intent);
Now I want to access string.xml file in ProgramClass but getResources(), getApplicationContext(),getContext() are not able to get me that stringArray defined string.xml file.
As it generating an error, the possible solution I have is to create a constructor & pass context to ProgramClass from MainActivity().
but for doing that I need to create an object of ProgramClass in MainActivity while using an explicit Intent which takes .class parameter. How can I start an intent or another activity by passing context of MainActivity?
....
NewActivity.setParams(context);
Intent i = ..........
in NewActivity:
private static Context c;
public static void setParams(Context c){
this.c = c;
}
This is a slight workaround because it uses static methods. This is however accessable from anywhere so any activity can change the context.
ABOUT INTENT
Intent is Android's native way to change from one activity to another. These do not change anything in the target/starting point class. They do however trigger the next step in the Activity Lifecycle:
There is no way to pass the context to the target activity using Intent. However, you can pass context, integers, booleans, strings, instances and so on using a static method as shown above. It works as long as you do not alter the context from the NewActivity class.
Also, I would like to add that all classes that extend Activity, Application, AppCompatActivity and so on are contexts. If you need to use context somewhere, you should pass it to a class without native context. If you are to use activities for main logic computing, try doing something that requires context and just write this.
I am starting other activity using intent from MainActivity. Here is my code:
Intent intent = new Intent(this, ProgramClass.class);
startActivity(intent);
Alright, that's great, but this won't work if ProgramClass doesn't extend some variant of Activity. And if it did, then you should have access to each of those methods you mentioned.
A hack would be that you define a static variable in your activity class and initialize it in onCreate method and later use that in your other class.
public static Context context = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
}
and then in your other class use:
MainActivity.context.getResources().getString();
I have a base activity containing action bar. I have options like: share, refresh in the action bar. But those methods are written in the fragments and have the instance of the fragments in another activity which is extending the base activity. So how to get the instance of the activity containing fragments?
In your SubActivity, define a member:
private SubActivity instance = this;
and also define a method:
public static Activity getSubActivityInstance(){
return instance;
}
In your BaseActivity, you can then use:
SubActivity mySubActivity = SubActivity.getSubActivityInstance();
if(mySubActivity != null){
// now call methods defined in SubActivity class
}
This will only work after an instance of SubActivity has been created (obviously), not before.
How do I get the instance of the currently visible activity in Android? I've read that I can get the ComponentName of the activity by using ActivityManager to get a list of tasks and messing with that, but that's a recipe for disaster.
Is there a way to get an instance of the topmost activity, or if the activity isn't mine and I can't access it, just null?
TO get instance of currently visible activity, get its context as:
Context mContext = getApplicationContext();
or
mContext = this;
now use it for this activity related tasks.
OR to get instance of another activity; keep a static ActivityClass instance somewhere else and use getter setter to get set this instance like:
public void setActivity(MyActivity activity) {
myActivity = activity;
}
public MyActivity getMyActivity() {
return myActivity;
}
Suppose I have a class first.java (activity class) and I start another activity in this class (second.java - activity class).
How can I access the instance of first.java from second.java?
Can someone give me a good explanation on this... An example would be great...
If you need your second activity to return some data to your first activity I recommend you use startActivityForResult() to start your second activity. Then in onResult() in your first activity you can do the work needed.
In First.java where you start Second.java:
Intent intent = new Intent(this, Second.class);
int requestCode = 1; // Or some number you choose
startActivityForResult(intent, requestCode);
The result method:
protected void onActivityResult (int requestCode, int resultCode, Intent data) {
// Collect data from the intent and use it
String value = data.getString("someValue");
}
In Second.java:
Intent intent = new Intent();
intent.putExtra("someValue", "data");
setResult(RESULT_OK, intent);
finish();
If you do not wish to wait for the Second activity to end before you do some work in the First activity, you could instead send a broadcast which the First activity reacts to.
You can simply call getParent() from the child activity.
I have no clue why other answers are so complicated.
Only this should work
class first
{
public static first instance;
oncreate()
{
instance = this;
}
}
first.instance is the required thing that is accessible from the second class
try this if this work 4 u.........
something like this.....
class first
{
public static first instance;
oncreate()
{
instance=this;
}
public static getInstance()
{
return instance;
}
}
now from second class call first.getInstance();
you can also directly acess instance in static way like this first.instance.......
Thanks...
You can't create an activity directly.
In the first activity take a static activity variable like this,
public static Activity activity;
In the onCreate do this.
activity = this;
Then in the second activity do this,
Activity activity = (your activity name).activity;
Edit:
For passing data from one activity to other activity this is not the way.
Above answer was to get activity instance from other activity which was initially asked.
To pass data from one activity to other activty generally use bundle. But if the data is not primitive data type, then use object class which should implement parcelable or serializable interface. Then through bundle only parcelable list of objects we can pass.