Use of Context to start another Activity
Yes its different for different cases,
It depends on the scope. Suppose if you are creating a method in a global class that extends
Application
to create a Toast
that is used in every class of your Application
you can use getApplicationContext()
to create it.
If you want to create a view that is restricted to that particular Activity you can use Activity.this
Also if you want to create an AlertDialog in some inner class say AsyncTask
, then you have to use Activity.this
, because the AlertDialog
is to be linked to Activity
itself.
Also don't use getBaseContext()
just use the Context
that you are having. For getting further information for the same you can see this Answer
.
So, the answer to the real question is better to use Activity.this
to start a new Activity
.
Intent intent = new Intent(Current_Activity.this, Calling.class);
startActivity(intent);
Pass Context to another Activity
Create Object of The Helper class from your Activity and pass 'this' as the context, say
MyHelperclass helper=new MyHelperclass(this);
In the Helper class Get this Context via its constructor
Context context;
MyHelperClass(Context context){
this.context=context;
}
Now You can pass this context to the makeText() method of Toast class.
how to start activity using context.startActivity(intent);...?
Try with this:
((Activity)context).startActivity(intent);
How to get context of another activity which is not started yet ?
There's no point or advantage to doing this. Each Activity
has its own lifecycle and its own version of Context
which exists only for the activity's lifecycle.
How to pass context from activity to activity?
Extending the Application class helps you to allow declare/access global variables. You can set your variables from any activity to ApplicationContext
and access it from other activity without using bundle.
How to declare global variables in Android? will help you.
Related Topics
Android: How to Implement First Time Tutorial Like Go Launcher in My App
How to Add Apache Commons Collections in Android Studio (Gradle)
CSS - Background-Size: Cover; Not Working in Firefox
Bordercolor Not Picking The Lineargradient Color on Android
Mat (Eclipse Memory Analyzer) - How to View Bitmaps from Memory Dump
Error Inflating Class Android.Support.V4.View.Viewpager
How to Paging Query from Firebase Using Android Firebaseui
Get Color Value Programmatically When It's a Reference (Theme)
How to Keep The Oauth Consumer Secret Safe, and How to React When It's Compromised
Error Inflating Class Com.Google.Android.Maps.Mapview
How to Develop Android App Completely Using Python
Why Should We Use Sp for Font Sizes in Android
Why Did The Listview Repeated Every 6Th Item
How to Take Emulator Screenshots Using Eclipse
How to Know If a Specific User Has Rated a Android App