using actionBar.setDisplayHomeAsUpEnabled in xml - android

I am writing this app and I seem to be calling actionBar.setDisplayHomeAsUpEnabled(true) in every Activity I create. I'm wondering if there is a way to set this value in the styles.xml or theme.xml; rather than repeating the same line of code every time.

Another way is you can declare in your AndroidManifest.xml
<activity
android:name=".CurrentActivity"
android:label="#string/title_activity_detailed_forecast"
android:parentActivityName=".MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.youpackage.name.appname.MainActivity" />
</activity>
CurrentActivity is your activity that you are in and MainActivity is your previous activity

Related

LAUNCHER activity is not loaded for the first time in android

I Have the Code in manifest:
<activity android:name=".activities.login.LoginActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".activities.main.screen.MainActivity"
android:exported="true"
android:launchMode="singleTop"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateHidden" />
What is happening:
The i load the app for the first time (Ex: Install it from
androidStudio) 'MainActivity' is loaded but once i close and launch
again LoginActivity is launched everytime
This happens for the first time
MainActivity launching happening for the first time
Its should always load 'LoginActivity' right since my launcher is
'LoginActivity' why is LoginActivity loaded just for first time. Is
there any setting i need to check
You have used
android:launchMode="singleTop"
in your main activity. According to docs -
if an existing instance of the "singleTop" activity is in the target task, but not at the top of the stack, or if it's at the top of a stack, but not in the target task — a new instance would be created and pushed on the stack.
So that's why MainActivity is loading.
You should remove
android:launchMode="singleTop"
to get desired results.
For more information on launch modes refer docs -
https://developer.android.com/guide/topics/manifest/activity-element

Up navigation goes to main activity

I have the following Androidmanifest file
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<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=".First"
android:label="#string/title_activity_first"
android:parentActivityName=".MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="android.anoop.com.myapplication.MainActivity" />
</activity>
<activity
android:name=".Second"
android:label="#string/title_activity_second"
android:parentActivityName=".First" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="android.anoop.com.myapplication.First" />
</activity>
</application>
In main activity's onCreate method i have the following code to launch Second activity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent1=new Intent(this,Second.class);
startActivity(intent1);
}
Other than that every other activities code is what android studio provide as default. But when i press up button from the Second activity it goes directly to main activity and not to the First activity. I know there are alternate solutions for this problem but i just want to know why the up navigation is behaving like this.I am new to android so i may not be understanding something really simple. Any help will be appreciated. Thank you.
If you are using calling SecondActivity in onCreate of FirstActivity, then it goes to SecondActivity. But think about it, when do an Activity actually starts?
It starts after you call onCreate, then onStart , then onResume and then your Actiivity finally starts running. In your case, you are not allowing to go further onCreate, and hence FirstActivity has never even started. And thats why when you go back in navigation it jumps to the previously opened Activity, i.e., MainActivity.
I hope this clears your answer! For reference see the Lificycle of an Activity in this link.
Because FirstActivity is not started or Created. You are calling Second directly from Main Activity. As there is no any First Activity available in application's stack how Second Activity goes to parent Activity First? It will goes to Main Activity as Its the only Activity remain in stack.

Android parent activity

I have this in my manifest:
<activity
android:name=".PimMediaActivity"
android:hardwareAccelerated="true"
android:screenOrientation="portrait">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".SettingsActivity" />
</activity>
On my PimMediaActivity which has SettingsActivity declared as its parent in the manifest, I have a custom back button. There are cases like from push notification where I need to start the PimMediaActivity directly, but I want to be able to go back to the parent activity when the button is clicked. Is this feature only working with the ActionBar back button ?!
You should put this :
<activity
android:name=".PimMediaActivity"
android:hardwareAccelerated="true"
android:screenOrientation="portrait"
android:parentActivityName=".activities.SettingsActivity" >
<!-- Parent activity meta-data to support 4.0 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".SettingsActivity" />
</activity>
Are you interested in retrieving the app state from before opening notification?
If no, you can just call startActivity(Intent) in onClick() method..

How to setup activities in AndroidManifest.xml

I have an activity, which is launched every time user wants to unlock a phone (MainActivity).
I wish to add another activity to the app, which will launch every time user clicks an icon of an app, and will contain settings for the first activity. What is the correct way to set it in AndroidManifest.xml?
Currently my AndroidManifest file looks like this:
<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" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".BootCompletedReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service android:name=".LockService"></service>
</application>
Define your activity in manifest like following:
<application>
...
<activity android:name=".YourNewActivity"></activity>
...
</application>
P.S:I assume that you activity is directly under the outermost package. if there are sub packages then you might need to use .subpackagename.YourNewActivity.
Now in your MainActivity, define a button inside who's onClickListener, you can start your second activity YourNewActivity using `Intents'. You might want to see this
How to start new activity on button click . Hope this helps.
You can't tie an activity to a button click in the UI inside the manifest file itself. Just add a normal <activity> and then ask for that activity to be called when you click the button.
The whole purpose of activities is that they can be re-used when the user opens the application again. You could create one activity and create a fragment everytime you open your app. Fragments do not have to be declared in your manifest. Your activity keeps track of the data. You are trying to add something dynamicaly (a unknown amount of activities) in a static xml-file (your manifest).
Just create a new fragment in your activity's onResume method.
http://www.vogella.com/articles/AndroidFragments/article.html

Perform search on Android within the same activity

I'm trying to integrate search in my app but I'm being blocked by one issue. My activity is declared like this and is located inside of TabActivity.
<activity
android:name="FileBrowserActivity"
android:screenOrientation="portrait"
android:launchMode="singleInstance">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="#xml/searchable"/>
</activity>
As you can see its launchMode is set to singleTop. But when I invoke search, input the search query and press enter another instance of such activity is launched. No onNewIntent is being called! But in the launched activity each new search doesn't launch a new activity.
The desired behaviour is to stay within current activity. Basically that's how android launcher search works.
You should rather use android:launchMode="singleTop" like described in this article.
You could use android:noHistory http://developer.android.com/guide/topics/manifest/activity-element.html#nohist

Categories

Resources