Calling an activity from another class - Android - android

How to call an activity from another class to a non-Activity class?
My code is as follows (Activity Class)
public void onCreate(Bundle savedInstanceState){super.onCreateSavedInstanceState);
this.mp();
}
public MediaPlayer mp(){//insert method here// }
Then in my non activity class i call
Intent i = new Intent();
i.setClassName(".......process", ".....ActualRenderingMedia");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
however if I try to use context.startActivity it will give an error asking me to create the activity method. I can't use getApplicationContext.startActivity either.

Is your non-Activity class instantiable? If so, you can add a constructor to the class that accepts a Context Object, and instantiate it from your main Activity.
For example, in you non-Activity class:
public class MyClass {
Context context;
public MyClass(Context context) {
this.context = context;
}
public void someOtherMethod() {
Intent i = new Intent(...);
context.startActivity(i);
}
}
And in your main Activity:
MyClass myclass = new MyClass(this);
...
myclass.someOtherMethod();

Related

Calling Intent from AsyncTask in another class

I've 2 activities(LoginActivity and VitalListActivity) and a class(PostLoginData) in another package. From LoginActivity I'm calling PostLoginData which extends AsynchTask. I'm trying to create intent in the onPostExecute() of the AsyncTask.
Here's the code:
protected void onPostExecute(String result) {
Log.d("ON POST EXE", "Success");
Intent intent = new Intent(LoginActivity.context, VitalListActivity.class);
Log.d("INTENT STARTED","SUCCESS");
try {
JSONObject json = new JSONObject(result);
String status = json.getString("status");
intent.putExtra(LoginActivity.EXTRA_MESSAGE, status);
Log.d("PUT MESSAGE", "Success");
LoginActivity.context.startActivity(intent);
} catch (JSONException e) {
e.printStackTrace();
}
}
It has no error, but when I run the app stops working. It has problem with the intent creation. So, how can I create an Intent from Asynch task of another class.
you need to inject the Context into the AsyncTask something like:
class MyTask extends AsyncTask<...> {
Context context;
MyTask(Context context){
this.context = context;
}
}
Then you can just use the context to start the Service.
You should consider using ApplicationContext and not Activity in that case
By the way i have no idea how LoginActivity.context does compile for you. the Activity class does not have a static Context unless you defined one by yourself which is not a very good idea
You have to create one constructor in PostLoginData class like below and passed LoginActivity Context on it.
public class PostLoginData extends AsyncTask<...>
{
Context m_context;
public PostLoginData(LoginActivity activity)
{
m_context = activity;
}
}
Replace below line in onPostExecute() method
Intent intent = new Intent(LoginActivity.context, VitalListActivity.class);
To
Intent intent = new Intent(m_context, VitalListActivity.class);

Starting an Android Activity from a static method

I want to start an activity from a static java method on an android device.
I do not have any context or anything passed as parameter to the static function.
For starting the activity I must call "startActivity" with the current running method as "this" pointer. So is there a way to get the current running activity?
You can access only static variables/objects inside static method.
So You need to Implement this way
public class MainActivity extends Activity {
private static Context mContext;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = this;
}
public static void goToLoginActivity() {
Intent login = new Intent(mContext, LoginActivity.class);
mContext.startActivity(login);
}
}
NOTE : But this is not the proper way to do so, this may cause window leak issue.
Better approach is pass activity/context object as parameter like this.
public static void goToLoginActivity(Context mContext) {
Intent login = new Intent(mContext, LoginActivity.class);
mContext.startActivity(login);
}
Create a Class in your app extending class Application, define a static context and initialise this with your application context. You can expose a static method from this class for accessing defined static reference. Thats it.
class MyApp extends Application{
private static Context mContext;
public void onCreate(){
mContext = this.getApplicationContext();
}
public static Context getAppContext(){
return mContext;
}
}
Now you can use this static method for accessing context anywhere in your app.

How to execute long job and then update widget from widget onclick method?

I'm logging to bank account and getting account balance.
I calling this function from onUpdate in widget and running in AsyncTask
package com.example.oobe.widget.widgetexample;
public class ExampleAppWidgetProvider extends AppWidgetProvider
{
(...)
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
{
(...)
if (notFromAsyncTask)
new MyAsyncTask().execute(context);
(...)
}
(...)
}
In method onPostExecute I want to call onUpdate widget and putExtra strings.
How can I do this?
package com.example.oobe.widget.widgetexample;
public class MyAsyncTask extends AsyncTask<Object, Void, BGZ>
{
Context context;
#Override
protected BGZ doInBackground(Object... params)
{
this.context = (Context)params[0];
return GetSomething();
}
protected void onPostExecute(BGZ page)
{
Intent intent = new Intent(context, ExampleAppWidgetProvider.class);
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
intent.putExtra("result", result);
intent.putExtra("webpage", webPage);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
(...)
}
If I doing like above I'm getting error:
.. Unable to find explicit activity class..
Can I do this from AsyncTask?
Can I call widget_update (onUpdate) with params to recognize that is from my AsyncTask?
Please give me little sample code (what to add to manifest if it must be broadcastreceiver and so on).
I have updated widget in onPostExecute but I think better method is to do that in ExampleAppWidgetProvider class?
If you are getting this error
Unable to find explicit activity class..
It may be due to this line
Intent intent = new Intent(context, ExampleAppWidgetProvider.class);
make sure ExampleAppWidgetProvider.class exist and
Context context = Activity.this;
or
Intent intent = new Intent(Activity.this, ExampleAppWidgetProvider.class);
UPDATE
public class ExampleAppWidgetProvider extends AppWidgetProvider
{
//your code .......
//asynctask as an inner class
public class MyAsyncTask extends AsyncTask<Object, Void, BGZ>
{
doInBackground(){}
onPreExecute(){}
onPostExecute(){
//save result
// or call your methods you need to run after your async call
}
}
}

How to start Activity from java class in Android

Here is the code.
public class MyActivity extends Activity {
#Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
MyClass myClass = new MyClass(this);
Cursor cursor = myClass.getCursor();
startManagingCursor(cursor);
}
}
public class MyClass Extends ImageButton {
private Context context;
public MyClass(Context context) {
this.context = context;
}
public Cursor getOncreate() {
Intent intent = new Intent();
intent.setClassName(MyClass.this,MyActivity.class);
context.startActivity(intent);
}
}
I want to start MyActivity from MyClass. I am getting NullPointer exception at
ctx.startActvity(intent);
Could you please suggest me how to make it correct.
try calling
startActivity(intent);
instead of
ctx.startActivity(intent);
Let one of the parameters of the start activity method be of type Context:
public void methodStartActivity(Context context) {
Intent intent = new Intent(context, MyActivity.class);
context.startActivity(intent);
}

Passing Values Between Activities Via Using Intents

I want my activity to pass a value to another activity class that extends view. Can you please tell me a solution? I am new to Android Programming.
Better idea is..
class CustomView extends View{
YourData data;
public CustomView(YourData data, Context context){
this.data = data
}
}
and in Activity
new CustomView(data. this)
pass throgh constructor or getter/seller . nothing related to android .
use standard java tequnique to pass data to object .
you can't because intents are used to comunicate between activitys, receivers & services.
You can pass the activity reference in the view and use that to get the appropriate values.
class MyActivity extends Activity {
MyView v;
int i;
public void onCreate(){
v = new MyView(this);
}
}
class MyView extends View {
MyActivity activity;
public MyView(MyActivity act) {
this.activity = act;
}
public void someMethod() {
int valueFromActivity = activity.i;
}
}
In the main class (A.java)
Intent i = new Intent(A.this,B.class);
i.putExtra("val", the value that you want to pass);
startActivity(i);
in B.java
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
Integer number = getIntent().getIntExtra("val", 0);
We will get the passes value in number

Categories

Resources