Default Intent for launcher Activity - java

What is value of Intent of launcher activity
hello friends, I'm new to android. I'm working on android activity and Intent. As i did in my launcher activity
Intent in = new Intent(getApplicationContext(), SecondActivity.class);
startActivity(in);
and in second activity i use getIntent() method to get an reference of calling intent in OnCreate method as:
Intent in = getIntent();
if(in != null) //do something..
In second activity getIntent() method return the reference of calling intent. But when I Use getIntent() method in onCreate method of launcher activity, its value was not null. So what reference is passed to intent when we use getIntent() method in launcher activity

Well the Android OS starts the activity when you click your app_icon, it looks through your manifest file and finds the launcher activity and then launches it

Get the intent which has started your activity using the getIntent() method:
Intent intent = getIntent();
Your data is represented as strings, then you can use intent.getStringExtra(String name) method. In your case:
String id = intent.getStringExtra("id");
String name = intent.getStringExtra("name");

Related

how to open fragment from another activity android

i am trying to use startactivityfromfragment()
i didnt find sumthing usefull for me to be a good example
until now i was able to move data(string of web url) from fragment to fragment
now its a diffrent story.. i think i need to use startactivityfromfragment()..
but dont know how
Bundle bundle = new Bundle();
bundle.putString("WEB","someweb.com");
MultiFragment multiFragment = new MultiFragment();
multiFragment.setArguments(bundle);
Intent intent1 = new Intent(getBaseContext(), MultiFragment.class);
startActivityFromFragment(MainActivity.class,intent1,"WEB");
Try this:
Put this code in current fragment, from where you want to open an activity.
Intent i=new Intent(getActivity(),Multifragment.class);
i.putExtra("key","value");
getActivity().startActivity(i);
In the next activity which contains fragment, put these:
String value=getIntent().getStringExtra("key");
Bundle bundle=new Bundle();
bundle.putString("key",value);
YourFragmentClass ob=new YourFragmentClass();
ob.setArguments(bundle);
getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.your_container_layout,ob).addToBackStack(null).commit();
Then, In YouFragmentClass:
Bundle=getArguments();
if(bundle!=null}
String value=bundle.getString("key");
Use your Activity name as second parameter in which the your MultiFragment exist
Intent i = new Intent(MainActivity.this, ActivityName.class);
putExtras(bundle);
startActivity(i);
finish();
Method 1
For sending data from activity to another activity's fragment call a normal intent with bundle value, In another activity check on getIntent() and call a method from that fragment
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("web_url", "url");
startActivity(intent);
and call from another activity
String webUrl = getIntent().getStringExtra("web_url");
Fragment newFragment = getSupportFragmentManager().findFragmentById(R.id.frame_tab);
newFragment.openWeb(webUrl);
Method 2
In second activity make a static variable and save the data from getIntent() and in fragment use that variable.
Mehtod 3
interface FragmentListner{
void openFragment(String url);
}
Activity A :
FragmentListner frag = new FragmentListner();
frag.openFragment("Url");
Activity B:
class B implements FragmentListner{
void openFragment(String url){
//Open Fragment here
}
I think:
In an activity that doesn't contain fragment:
Intent intent = new Intent(ActivityA.this, ActivityB.class);
intent.putExtra("something", "yep");
startActivity(intent);
And, in the next activity which contains fragment:
Intent intent = getIntent();
String op = intent.getStringExtra("something");
if (op != null){
getSupportFragmentManager().beginTransaction().replace(R.id.nav_host_fragment,
new YourFragment()).addToBackStack(null).commit();
}

How do I get the reference of an activity I opened using intent?

Inside my MainActivity I open my SecondActivity
Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);
How do I get the reference of this instance of SecondActivity inside my MainActivity?
//inside MainActivity
SecondActivity object = // make it equal to instance created above
Pass your a handler to the second one via trigger intent, like this:
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("Handler", handler);
startActivity(intent);
Then in your SecondActivity post message and data, maybe the instance of SecondActivity.this or another you like, to the handler.

pass Intent to function throws NullPointerException

In an Android I would like to pass an Intent Object to a function, but consistently get a NullPointerException.
I could not find any source citing that this is not possible.
basically I have this:
public Intent intent;
/**
* Called when the activity is first created.
*/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
intent = getIntent();
int check = elsewhere.chk_intent(intent); //<=THROWS ERROR
}
chk_intent() performs some functions on data transmitted with the intent, especieally if some extra fields are present.
I tried to move getIntent() into this function, but this is not allowed.
Any help would be much appreciated
Is this activity called by another activity? The intent will not be null only if previous activity supplies an intent to the current activity:
// previous activity
Intent intent = new Intent(FirstActivity.class, SecondActivity.class);
int value = 10;
intent.putIntExtra("key", value);
startActivity(intent);
// use this if want to return data
//startActivityForResult(intent);
If this activity is the starting (main) activity or it is not called by any previous activity, then getIntent() is null, you need to initialize your own new Intent object.
// current activity in onCreate() method
if(getIntent() == null) {
Intent intent = new Intent();
int check = elsewhere.chk_intent(intent);
}

How to close all running activities from another one?

I've got some activities which I execute one after another without closing (1->2->3->4). I want to close 1,2,3 activities from 4 and execute a new one; the code:
Intent intent = new Intent(UserpickActivity.this, CommunicationActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
UserpickActivity - 4th activity, CommunicationActivity - the next activity. But after this code all previous activities still worked. How can I close all running activities and execute some new activity?
Try this:
When you want to launch CommunicationActivity from Activity4, do this instead:
Intent intent = new Intent(this, Activity1.class); // Your root activity
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); // Will remove all other activities from the task
intent.putExtra("foo", true); // This is to tell Activity1 to launch Activity4
startActivity(intent);
In Activity1.onCreate() do the following:
super.onCreate();
if (getIntent().hasExtra("foo")) {
// We should now launch CommunicationActivity and finish ourselves
Intent intent = new Intent(this, CommunicationActivity.class);
startActivity(intent);
finish();
return; // Don't continue the rest of onCreate()
}
// The rest of your onCreate() goes here
You can only use CLEAR_TOP to clear down to an existing activity in the activity stack. That's why your use of CLEAR_TOP isn't working (because CommunicationActivity doesn't exist in the activity stack).
This method clears all activities and launches the root activity again. When the root activity's onCreate() is called, it checks the Intent for the extra "foo", which it uses as a signal that it needs to launch CommunicationActivity and finish itself. This should do what you want.
Call activity.finish(); after you switch to other activity. This way no activity will remain on stack.
Intent intent = new Intent(UserpickActivity.this, CommunicationActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
//YOUR_ACTIVITY.finish();
Uncomment the above line if you want this activity to be removed from activity stack.
// This will clear all activity and start new task.
Intent intent = new Intent(UserpickActivity.this, CommunicationActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
Use android:nohistory=true in manifest for Activities.
<activity
android:name=".A"
android:label="#string/app_name"
android:noHistory="true" >

Passing data between 2 activities

I am programming in android for a Samsung tablet, and I have 2 activities, one of this is a list of football teams, and the other activity is their twitter, but i have a problem to pass the parameter for the first activity to the second one. I want to pass their url like a string, but i cant get it.
Thanks!
You have to use Intent:
Intent i = new Intent(this, SecondActivity.class);
i.putExtra("extraURL", "http://myUrl.com");
startActivity(i);
Then, to retrieve it in SecondActivity, in the onCreate method do:
Intent receivedIntent = getIntent();
String myUrl = receivedIntent.getStringExtra("extraURL");
You typically launch the 2nd Activity from the first using an Intent. You can pass data to the 2nd Activity using the same Intent you use to launch it. For example,
Intent i = new Intent(this, SecondActivity.class);
i.putExtra("url", "http://url.you.want.to.pass/");
startActivity(i);
In the 2nd Activity, in the onCreate, you can read the data using:
Intent i = getIntent();
String url = i.getStringExtra("url");
your First Activity on click of button
Intent intent = new Intent(this,ActivityTwo.class );
intent.setAction(intent.ACTION_SEND);
intent.putExtra("www.google.com",true);
startActivity(intent);
Receive it in ActivityTwo:-
Intent intent = getIntent();
String msg = intent.getStringExtra("www.google.com");

Categories