I can't seem to get my extra information to get sent along with an intent. The intent loads the next activity properly, but without the payload.
Caller:
//a list item was clicked
protected void onListItemClick(ListView l, View v, int position, long id)
{
super.onListItemClick(l, v, position, id);
Intent i = new Intent(this, ProgramTracks.class);
i.putExtra("_id", id);
startActivity(i);
}
Receiver:
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.program_tracks);
if( savedInstanceState != null )
{
mDateId = savedInstanceState != null ? savedInstanceState.getLong("_id") : null;
Toast.makeText(this, "ID " + mDateId, Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(this, "Null!", Toast.LENGTH_SHORT).show();
}
}
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.site.android.app"
android:versionCode="1" android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".MainActivity" android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ProgramTracks" android:label="#string/app_name">
</activity>
</application>
</manifest>
I'm not sure what I'm missing, but my if keeps Toasting null.
Edit: If I Toast or Log the id variable that gets putExtra()'d, it is set.
In your original activity, you put "_id" as an extra in your intent, but in the second activity you are attempting to retrieve the long from the savedInstanceState.
Instead you should do something like this:
Intent myIntent = getIntent();
Long id = myIntent.getLongExtra("_id", 0);
Where 0 is the default value if no long is found with the tag "_id".
Here's another question about the difference between getting extras from savedInstanceState and Intents.
And here's the developer page on putting/getting extras in Intents.
getIntent().getExtra() will give you the bundle
Then from bundle you can get the required value
Try this:
Bundle bundle = getIntent().getExtras();
long _id = bundle.getLong("_id");
In your Receiver's onCreate() you need to get the passed variable using
Bundle extras = getIntent().getExtras();
mDateId = extras.getLong("_id");
savedInstance is used only when your Activity is being recreated.
Neeraj was almost right. Use getIntent() to retrieve the data passed with the intent used to start the activity.
mDateId = getIntent().getLongExtra("_id", -1);
savedInstanceState is used to retrieve data saved when the activity is suspended.
try this
Intent i = new Intent(MainActivity.this, ProgramTracks.class);
use mDateId = intent.getLongExtra("_id", -1);
if(mDateId != -1)
Toast.makeText(this, "ID " + mDateId, Toast.LENGTH_SHORT).show();
Related
In my application I have several "intents" that I use to transition between different activities in my application. I have noticed a strange behavior that occurs on Samsung devices - but not on Nexus devices - whenever a new intent is created the application launches a second "task" for this new activity! When the user goes to the multi-tasking menu they can see multiple copies of the application! This is not the desired behavior. Any and all advice would be greatly appreciated!
Manifest:
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme"
android:launchMode="singleInstance">
<activity
android:name=".MainActivity"
android:screenOrientation="portrait"
android:launchMode="singleInstance">
</activity>
<activity
android:name=".Settings_area"
android:screenOrientation="portrait" />
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="AIzaSyDieXTCaFoIL0kJ_IM4UMBSQL3sNn92AWM" />
<activity
android:name=".MapsActivity"
android:label="#string/title_activity_maps" />
<activity android:name=".Splash"
android:launchMode="singleInstance">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".aboutPageActivity" />
<activity android:name=".turnOffFromNotification"
android:noHistory="true"></activity>
</application>
I have already attempted removing the launch modes as well as changing the application launch mode to singleTop and standard.
Intent that creates a second instance:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
new Handler().postDelayed(new Runnable(){
#Override
public void run() {
/* Create an Intent that will start the Menu-Activity. */
Intent mainIntent = new Intent(Splash.this,MainActivity.class);
Splash.this.startActivity(mainIntent);
Splash.this.finish();
}
}, splashDisplayLength);
return;
}
Intent that creates a third instance:
public void goToAboutPage()
{
Intent goToAboutPage = new Intent(this, aboutPageActivity.class); //create the intent to go to the map screen
startActivity(goToAboutPage); //actually go to the map screen
}
A third instance can also be created from launching a settings intent:
public void changeToSettingsScreen() //changes the screen to the setting screen
{
readyToSendPackets = false;
sendSwitch.setChecked(false);
// textView.setText("NOT sending"); //set the textview to advise users packets are not being sent
Intent goToSettings = new Intent(this, Settings_area.class);
startActivity(goToSettings);
}
I also over rode the onNewIntent Method:
protected void onNewIntent(Intent intent) {
// super.onNewIntent(intent); //REMOVED THIS TO AVOID DOUBLE INSTANTIATION ON TOUCHWIZ IF ANYTHING BREAKS LOOK HERE FIRST
setIntent(intent); //this allows us to recieve the extras bundled with the intent
// System.out.println("Here is the bindle: " + getIntent().getExtras());
if (getIntent().getExtras() != null) //check to see if there are any extras, there wont be on apps first start
{
Bundle extras = getIntent().getExtras(); //get the extras
String methodName = extras.getString("methodName"); //assign the extras to local variables
if(methodName != null && methodName.equals("turn_send_switch_off"))
{
sendSwitch.setChecked(false);
}
//else if(**other actions that may need to be performed can go here**)
}
Thank you very much for any help!!!
Usually if you have to force a single instance of the app, you should avoid putting android:launchMode="singleInstance" on each activity seeing as it would try to launch an instance for each activity.
Removing the launchMode from everything except the application should ensure that only the application runs in a single instance, although what #Shaishav said is true, most of the time you can let android deal with the lifecycle of an application by not setting the launchMode, unless you have a real need to ensure only one instance is running at a time.
In TabHost we can do getActionBar().setTitle("ACTITIVTY TITLE").
But what about in the intent?
Is there possible way to set title in intent?
code
Intent i = new Intent(MenuActivity.this, DrawerListActivity.class);
startActivity(i);
Toast.makeText(getBaseContext(), "RF number: " +
KEY_RFnumber, Toast.LENGTH_SHORT).show();
MainActivity.class
public void send(View view) {
Intent intent = new Intent(this, DrawerListActivity.class);
String message = "Drawer Title";
intent.putExtra("key", message);
startActivity(intent);
}
DrawerListActivity.class, in onCreate()
String message = getIntent().getStringExtra("key").toString(); // Now, message has Drawer title
setTitle(message);
Now, set this message as Title.
You can't set the title directly with the intent. You could pass along the title in the intent, and have your target activity extract the title from the intent and set it. This would only be a few lines of extra code in the target activity.
I got it! I just declare from my MainActivity a public static String that holds a string which I set in my Intent, then it call to my DrawerListActivity. It works perfectly! Thanks.
i just paste another person answer regarding this question may be it will help you
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name_full" >
//This is my custom title name on activity. <- The question is about this one.
<intent-filter android:label="#string/app_launcher_name" > //This is my custom Icon title name (launcher name that you see in android apps/homescreen)
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
I am trying to achieve following case on Android, but no success:
1) Launch Application (Launcher Activity which is a subclass of Base Activity). The Base Activity has code as follows:
///This is in BaseActivity
#Override
public void onCreate(Bundle instance)
{
super.onCreate(instance);
//Config.isLoggedIn() is a static function.
if(! Config.isLoggedIn())
{
////Config.startLoginActivity is a static function
Config.startLoginActivity(this, getIntent());
finish();
}
}
The Config.startLoginActivity functions is defined as
public static void startLoginActivity(final Context ctx, final Intent finishIntent)
{
Intent i = new Intent(ctx, ItemListActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra("FINISH_INTENT", finishIntent);
ctx.startActivity(i);
}
Now, the ItemListActivity contains a list of Items as {Item1, Item2, Item3}. In ItemListActivity, I am saving the passed "finishIntent" as
///This is ItemListActivity onCreate Method
if(getIntent().hasExtra("FINISH_INTENT"))
mFinishIntent = getIntent().getParcelableExtra("FINISH_INTENT");
and the onItemListSelected method is described as follows :
#Override
public void onItemSelected(String id) {
Config.setLogInState(true);
if(mFinishIntent != null)
{
Log.i("ITEMLISTACTIVITY", "Class Name = " + mFinishIntent.getClass().getName());
Log.i("ITEMLISTACTIVITY", "Starting mFinishIntent Activity");
startActivity(mFinishIntent);
finish();
}
}
But the issue is the Main Activity is not being launched again, Android takes me to the home screen instead. While looking for a solution, I saw that Google I/O app has the same implementation and that works flawlessly but in my case it is not. I am unable to figure it out. Please help.
Thanks in Advance.
Manifest File is as follows :
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.app.myapplication.ItemListActivity"
android:label="#string/app_name" >
</activity>
<activity
android:name="com.app.myapplication.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Ok Here is a quick help which works for 100 percent which I'm using not mostly but EVERYTIME! you must past it through intent and in your case here it is how it must look like.
Intent intent = new intent(//name of your activity in which you are at the moment.this, //name of activity to which you want to go.class);
startActivity(intent);
Hope this will help
I am able to send the package name using this code. But how can I start the new app by using its package name?
lv.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
//CheckedTextView item = (CheckedTextView) view;
//if(item.isChecked()){
packageName = list.get(position).activityInfo.packageName;
Log.i("PACKAGE", packageName);
//TODO: Pass this packageName to a method
finalList.add(packageName);
//}else if(!item.isChecked()){
//}
}
});
I'm going to show you how to send and receive implicit intents. This is useful for sending data to one or many activities, services or broadcast receivers and also between apps. The downside is that it is a 'public' intent (if you do not use the LocalBroadcastManager) so don't send any sensitive information.
Create a send-method that the sender uses.
Modify the AndroidManifest.xml for the receiving entity (In your case an Activity)
Modify the receiving entity to handle the incoming Intent.
Step one - Create a send-method that the sender uses.:
public final static String ACTION_SEND_NAME = "com.example.intent.action.SEND_NAME";
public final static String ACTION_SEND_NAME_EXTRA = "name";
public static void sendPackageName(Context context, String name) {
if (null == context || null == name) {
throw new IllegalArgumentException("Argument(s) may not be null");
}
Intent intent = new Intent(ACTION_SEND_NAME);
intent.putExtra(ACTION_SEND_NAME_EXTRA, name);
//You might add extra these flags
intent.addFlags(FLAG_ACTIVITY_CLEAR_TOP|FLAG_ACTIVITY_SINGLE_TOP);
//LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
//context.sendBroadcast(intent);
//context.startService(intent);
context.startActivity(intent);
}
Step two - Modify the AndroidManifest.xml:
<activity
android:name="com.example.MyActivity"
android:label="#string/app_name">
<intent-filter>
<!-- Other Intent filters -->
<action android:name="com.example.intent.action.SEND_NAME" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Step three - Modify the receiving entity:
void onCreate (Bundle savedInstanceState) {
//...
// Get intent, action and MIME type
Intent intent = getIntent();
String appName;
if (ACTION_SEND_NAME.equals(intent.getAction())) {
appName = intent.getStringExtra(Intent.ACTION_SEND_NAME_EXTRA);
}
You can do it by sending an intentExtra to the activity that you want.
Intent in = new Intent(getApplicationContext(), YourOtherClass.class);
in.putExtra(TAG_PACKAGE_NAME, packageName);
startActivity(in);
Then in the "YourOtherClass", get the value by calling :
Intent in = getIntent();
String packageName = in.getStringExtra(TAG_PACKAGE_NAME);
Hope that helps :)
EDIT:
To send a list , ArrayList , you can use :
in.putStringArrayListExtra("listOfValues", yourArrayList);
and get it using
in.getStringArrayListExtra(listOfValues);
I'm coding a simple android app where you write in a box your name then click ok and a new page will show your name... The problem is that when you click ok nothing happens.
Here the main activity
public class Click extends Activity implements OnClickListener{
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
String TypedText = (String)MyText.getText().toString();
Intent MyInt = new Intent(this, HelloWorld.class);
MyInt.putExtra("user", TypedText);
this.startActivity(MyInt);
Bundle Retrive = this.getIntent().getExtras();
Retrive.getString("user");
setContentView(R.id.Text);
TextView TextV = (TextView)findViewById(R.id.Text);
TextV.setText("user");
}
android.widget.EditText MyText;
public void OnCreate (Bundle savedInstanceState){
super.onCreate(savedInstanceState);
this.setContentView(R.layout.name_getter);
MyText = (EditText)this.findViewById(R.id.editText1);
this.findViewById(R.id.button1);
android.widget.Button RefBut = (Button)this.findViewById(R.id.button1);
RefBut.setOnClickListener(this);
}
And here the manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="edu.calpoly.android.lab1Sada"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="4" />
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".Click"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="HelloWorld" ></activity>
</application>
</manifest>
Starting the android emulator will launch the first activity click but then the app doesn't show the new view...
I dont know what you are doing but you can send/retrieve data from/to activity to another activity like this way:
For that you need to understand the concept of Intent.
From First activity:
Intent i = new Intent(this, ActivityTwo.class);
i.putExtra("name", "paresh");
i.putExtra("technology", "android");
startActivity(i);
From Second activity:
Bundle extras = getIntent().getExtras();
if (extras == null) {
return;
}
String strName = extras.getString("name");
String strTechnology = extras.getString("technology");
Still for your reference, here is the article to know more about the same: Android Intents
You must pass the text from activity 1 and receive it as bundle in activity 2.
Go through the helloworld program as your first tutorial for android.
Just extending Activity will work. You do not need to mention the entire package of the superclass.