How to call fragment from IntentService class? - android

I have a class named 'GeofenceIntentService' to receive Push Notification and I want to call fragment from that class, the class is extends IntentService class.
Thanks in advance.

If you need to open a screen, you should open an Activity which contains your Fragment, but not a Fragment instead.
For example:
Intent i = new Intent();
i.setClass(GeofenceIntentService.this, HostActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
If you need to communicate between Service and Fragment, look at Bound Service .
If you need to send a message to Fragment from the Service, use BroadcastReceiver

I solved the problem. if we are using this in Fragment its not find Activity context.
We are using MyFragment.this.getActivity()
Intent intent = new Intent(MyFragment.this.getActivity(),MyService.class);
intent.putExtra("path",uri);
MyFragment.this.getActivity().startService(intent);

Related

Perform overridePendingTransition to start an Activity from Service Class?

I'm unable to perform a overridePendingTransition to start an Activity from a service class. I'm opening an activity from service class which is working perfectly but I want to start that activity with a transition.
this code is written in service class
Intent intent = new Intent(Activity1.this, Activity2.class);
startActivity(intent);
activity.overridePendingTransition(R.anim.fade_in,R.anim.fade_out);
but overridePendingTransition is not working its shows an error log
"android.app.Application cannot be cast to android.app.Activity"
Thanks in advance!
try using overridePendingTransition(R.anim.fade_out,R.anim.fade_in);
in your Activiy --> OnCreate()
So it will always animate irrespective of from where it is been called.
You need to call overridePendingTransition method on your activity istance, it looks like activity here is your application instance.

Data passing between fragment activity to another activity

I want to send my location(address) from my fragment activity to another activity and show the address there. I tried for last couple days but not failed every time. please guide me.
Use Intent to pass data from one activity to another
Intent myIntent = new Intent(FirstActivity.java,SecondActivity.class);
myIntent.putExtra("data","**** Your data *****");
startActivity(myIntent);
If you want to pass user derfined Object from one activity to another then you have to implement Serilizable or Parcelable
Check the following links for how to use Serilizable
link1 & link2
use getActivity() to send data into other activity
Intent myIntent = new Intent(getActivity(),SecondActivity.class);
myIntent.putExtra("key","**** Your data *****");
getActivity().startActivity(myIntent);

Android create new instance of current activity

I an have activity called A in my project, there is a button that create new instance of the same activity. For example i want to do something like this:
intent = new Intent(this, A.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(MainActivity.EXTRA_DATA, data);
startActivity(intent);
If i use this code the app crashes when i push the button.
I have found the solution. The activity use a object of another class called data, this class need to implement Serializable.

Can we invoke one activity into another activity?

I Mean that i want to use one activity into another activity, Like class using create instance of that class. Is it Possible?
Well, I think you should use Intents to call an activity from another activity.
Call this from your Activity:
Intent in = new Intent(getApplicationContext(), NextActivity.class);
startActivity(in);
you can do it only by saying startActivity(), no other go. you can't make an instance of Activity because , an Activity gets created when its onCreate() method gets called, but when you say new MyActivity() its default constructor is called and not its onCreate() method (which Android OS will not accept). so always say startActivity() or startActivityForResult() which are handled by android OS
Write this code from where you want to run activity
Intent intent = new Intent(current_Activity_name.this,New_Activity_name.class);
startActivity(intent);
And add the following code into manifest file
<activity android:name=".New_activity_name" />
Well, since an Activity is a displayable-window, the appropriate concept would be that one Activity can be "launched" from another. This is how you achieve that:
Intent i = new Intent(CurrentActivity.this, NewActivity.class);
CurrentActivity.this.startActivity(i);
This code snippet can launch NewActivity from any point in the CurrentActivity code, for example, an 'OnClickListener'.
Yes, it is possible. This is achieved through Intents.
Intent intent = new Intent(this.getApplication(), TARGET_ACTIVITY_NAME.class);
//To add data use intent.putExtra(NAME,VALUE);
intent.setData(data.getData());
try
{
startActivity(intent); // This ll launch the TARGET_ACTIVITY_NAME
}
catch(Exception e)
{
}
For more information refer this link.
Shash

How do I start an Activity?

Suppose that, I have an android app that launches browser with some url supplied when it receives a message containing some prespecified data(some code or something)
For this to work, my class inherits broadcastReceiver class(to receive messages).
Now as soon as it recieves a msg, it needs to launch another activity i.e browser and for this the same class needs to inherit Activity class also. But it is not possible, a class can not inherit 2 other classes.
My problem looks roughly like,
import android.content.broadCastReceiver;
import android.app.Activity;
public class sms extends broadCastReceiver{
onReceive(){
....
....
here it needs to launch another activity
}
}
Could anyone suggest how I can implement this...?
I tried creating an instance of Activity subclass inside and invoking startActivity method, but it did not work
The method for starting an activity is aContext.startActivity(new Intent(aContext, MyNewActivity.class)); Be sure that you place the proper declarations in the manifest though.
Look here.
For clarification, you can start an activity using a context. So just keep a short term reference to one and you should be fine.
EDIT:
You need to have a reference of a usable context to even create an activity. Then you do the following (using the passed reference!)
Intent i = new Intent(passedContext, MyNewActivity.class);
i.setFlags(Context.FLAG_ACTIVITY_NEW_TASK);
passedContext.startActivity(i);
Here i can launch a new activity using,
Intent i = new Intent(Context, MyNewActivity.class);
i.setFlags(Context.FLAG_ACTIVITY_NEW_TASK);
Context.startActivity(i);
here, context was passed to onReceive method of broadCastReceiver as a parameter, i can use that context itself. I can not use getBaseContext method here.

Categories

Resources