how to set Activity Title in Intent? - android

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>

Related

Not Navigating back to MainActivity

When I click on Notification I should be navigated to Main2Activity and when I click on back button of Main2Activity I should be navigated back to MainActivitybut I am getting navigated back to Home screen.
Is there any mistake in my code?
NotificationCompat.Builder noti = new NotificationCompat.Builder(MainActivity.this);
noti.setContentTitle("Message for you!");
noti.setContentText("Hi!!This is message for you");
noti.setSmallIcon(R.drawable.ic_launcher_background);
noti.setTicker("app name:message app");
noti.setAutoCancel(true);
Intent intent = new Intent(MainActivity.this,Main2Activity.class);
TaskStackBuilder taskStackBuilder=TaskStackBuilder.create(MainActivity.this);
taskStackBuilder.addParentStack(MainActivity.class);
taskStackBuilder.addNextIntent(intent);
PendingIntent pendingIntent=
taskStackBuilder.getPendingIntent(1234,PendingIntent.FLAG_UPDATE_CURRENT);
noti.setContentIntent(pendingIntent);
Notification notification=noti.build();
NotificationManager notificationManager = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(1234,notification);
Mainifest.XML file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sainathpawar.notifications">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Main2Activity"
android:parentActivityName=".MainActivity">
<intent-filter>
<action android:name="second_filter" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
Please try this
<activity
android:name=".Main2Activity"
android:parentActivityName=".MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</activity>
It is because you are not within a stack. You only launched one activity. Your default MainActivity has not been created or launched.
You can handle the back button press with android.R.id.home in the OnMenuItemSelected callback and redirect them wherever you would like. You can try the "parent activity" route as well, but I'm not certain how that works when launched from notification without context of the parent to launch it to begin with.
If you go that route, update to let us know if it worked for you.
Otherwise you can easily use my answer as well.
EDITED FOR CLARITY
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
startActivity(Main2Activity.this, MainActivity.class);
return true;
finish();
}
return super.onOptionsItemSelected(item);
}
taskStackBuilder.getPendingIntent(1234,PendingIntent.FLAG_UPDATE_CURRENT);
noti.setContentIntent(pendingIntent);
There was error in coding at above line.If we see here the requestcode is 1234 which is same as ID of notify method
notificationManager.notify(1234,notification);
so it was navigating back to Home screen because Android OS was thinking as if its on MainActivity because of 1234 request code in getPendingIntent.
****Solution is:****change requestCode of getPendingIntent() method from 1234 to any random number I changed it to 0 and it worked for me.
You can achieve this by PendingIntent without TaskStackBuilder as:
Intent parentIntent = new Intent(this, MainActivity.class);
parentIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
Intent resultIntent = new Intent(this, Main2Activity.class);
final PendingIntent pendingIntent = PendingIntent.getActivities(context, 0,
new Intent[] {parentIntent, resultIntent}, PendingIntent.FLAG_UPDATE_CURRENT);
noti.setContentIntent(pendingIntent);

Starting the Main activity from another activity

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

Redirect to concreate activity with notifications

I'm using Notifications in Android. When the user clicks them, I have to open the application and redirect him to one specific Activity, it works fine if the user who gets the notifications didn't have the application opened. (I mean, opened in background), if he has the application opened, when he clicks the notification he is redirected to the "Main" activity when I want to redirect him to the same activity.
I guess that it could be some mistake in my AndroidManifest.xml,, but, I'm not sure, could you someone help me??
My manifest:
<application
android:screenOrientation="portrait"
android:debuggable="true"
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen">
<!-- android:theme="#style/AppTheme" android:theme="#android:style/Theme.NoTitleBar.Fullscreen" -->
<activity
android:name="com.trivialword.activities.MainDisplayActivity"
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="com.trivialword.activities.ResultActivity"
android:label="#string/title_activity_result"
android:theme="#android:style/Theme.Light.NoTitleBar.Fullscreen" >
</activity>
And here is where I configure the notification
Context contexto = context.getApplicationContext();
CharSequence title = "Trivial Invitación";
CharSequence description = "Invitacion a una partida del usuario " + msg;
Intent notIntent = new Intent(contexto,
GameOnePlayerPrivateOnlineActivity.class);
notIntent.putExtra("opponent", true);
notIntent.putExtra("who-create-game", msg);
PendingIntent contIntent = PendingIntent.getActivity(
contexto, 0, notIntent, 0);
notif.setLatestEventInfo(
contexto, title, description, contIntent);
Thank you!.
In intent declaration, try to write this.
http://developer.android.com/guide/topics/ui/notifiers/notifications.html
Creating a simple notification:
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(this, ResultActivity.class);
I think, that is the problem.

Linking two activity in android

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.

android open dialogue activity without opening main activity behind it

Im writing a program that offers a quick reply dialog upon receipt of an SMS.
However, I am getting an unexpected result. When I receieve an SMS, the appropriate dialog activity comes up displaying the correct phone number and message, however there is a second activity behind it that is the 'default' activity in my program (it is what opens when i launch my application)
I do not want this second activity to come up. The quick reply activity should come up by itself over top of whatever the user was doing before.
The 'floating' activity:
public class quickReply extends Activity {
String mNumber, mMessage;
TextView mMainText;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mMainText = (TextView)findViewById(R.id.mainText);
try{
Intent i = getIntent();
Bundle extras = i.getExtras();
mNumber = extras.getString("theNumber");
mMessage = extras.getString("theMessage");
this.setTitle("Message From:" + mNumber);
mMainText.setText(mMessage);
} catch(Exception e) {
mMainText.setText(e.getMessage());
}
}
}
The call to the activity inside an onReceive()
Intent i = new Intent(context, quickReply.class);
i.putExtra("theNumber", mNumber);
i.putExtra("theMessage", mMessage);
i.setFlags(
Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
The Manifest:
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".quickReply"
android:label="#string/app_name"
android:theme="#android:style/Theme.Dialog"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".SmsReceiver">
<intent-filter>
<action android:name=
"android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
the only way I have found that works, in your activity definition in manifest:
android:launchMode="singleInstance"
but then you have to relaunch your main/default activity once the dialog is dismissed. NOTE: you will lose all state from the previous launch, so this is a less than ideal solution.
UPDATE:
you can also do this by:
Intent.FLAG_ACTIVITY_CLEAR_TASK
so here's what I did:
open the original/main activity
from a service, launch the dialog style activity using the above (main goes bye-bye).
when the user dismisses the dialog, start main again with an extra intent (IS_BACK) that is processed in onCreate() and calls:
moveTaskToBack(true);
this will keep the task under the dialog on top and your main in the back of the stack.
You should set the task affinity of the activity to something different than your main activity. This will separate it from the main activity and it will track as a separate task:
<activity android:name=".quickReply"
android:label="#string/app_name"
android:theme="#android:style/Theme.Dialog"
android:launchMode="singleTask"
android:taskAffinity="quickReply"
>

Categories

Resources