Up Button - getSupportActionBar().setDisplayShowHomeEnabled(true) - closes app - android

I've tried to make a "return" button for my app, or as Google call it, Up Button for Low-level Activities.
I wrote everything properly:
in My XML I put the meta-data and the parent activity (Example from Google, I modified the text to my app in my files)
android:parentActivityName="com.example.myfirstapp.MainActivity" >
<!-- Parent activity meta-data to support 4.0 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myfirstapp.MainActivity" />
in My class I put the next line:
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
but when I click this up button, the app closes, then opened again from the main screen.
I want it to return to the main screen without closing (finishing) the app.
Can you help me? Thanks.

Well, according to this guide,
When running on Android 4.1 (API level 16) or higher, or when using ActionBarActivity from the Support Library, performing Up navigation simply requires that you declare the parent activity in the manifest file and enable the Up button for the action bar.
So, I have two Activities: FirstActivity, which is my launcher and parent Activity and SecondActivity, which is my child Activity.
My FirstActivity code:
public class FirstActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_main_activity);
Button secondButton = (Button) findViewById(R.id.secondButton);
secondButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(FirstActivity.this, SecondActivity.class));
}
});
}
}
My SecondActivity code:
public class SecondActivity extends ActionBarActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_place_activity);
//enable the ActionBar behaviour
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
}
}
My Manifest.xml file:
<?xml version="1.0" encoding="utf-8"?>
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="20" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.testes.activity.FirstActivity"
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.testes.activity.SecondActivity"
android:parentActivityName="com.testes.activity.FirstActivity" >
</activity>
</application>
</manifest>
And this is all I need to have it working your way. I start my FirstActivity, click my Button to go to the SecondActivity, click the ActionBar home button and it goes back to FirstActivity.

Related

Pressing phone back button in one activity goes to another activity that hasn't been initialized before Android

This is very different from all of the "similar" questions SO has given me. I am working on an app and when a user signs up an account in one activity called SignUpActivity, I take them to another activity called UserProfileActivity using the following code:
SignUpActivity:
Intent userProfileIntent = new Intent(SignUpActivity.this, UserProfileActivity.class);
startActivity(userProfileIntent);
finish();
Now, in UserProfileActivity, if I press the back button, it takes me to an activity that has never been visited/instantiated during the app's lifetime called PartyListActivity.
UserProfileActivity:
public class UserProfileActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_profile);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
}
So my question is why, when I press back in UserProfileActivity, does it go back to PartyListActivity, when I have never been on PartyListActivity before? Is there something in the manifest file that dictates this behavior?
Edit
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sample.package">
<!-- To auto-complete the email text field in the login form with the user's emails -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<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/Theme.AppCompat.Light.NoActionBar">
<activity
android:name=".LoginActivity"
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=".SignUpActivity"
android:label="#string/title_activity_sign_up" />
<activity
android:name=".PartyListActivity"
android:label="#string/title_activity_party_list"
android:theme="#style/AppTheme.NoActionBar" />
<activity
android:name=".UserProfileActivity"
android:label="#string/title_activity_user_profile"
android:theme="#style/AppTheme.NoActionBar" />
<activity
android:name=".FriendsActivity"
android:label="#string/title_activity_friends"
android:theme="#style/AppTheme.NoActionBar" />
</application>
</manifest>
I'll try my best to break the stuff and follow the activity flow:
Your launching as well as root activity is LoginActivity
( From your manifest file).
Then, from LoginActivity you entered to SignUpActivity, either with some UI action or programmatically (Assuming the LoginActivity is not finished).
From SignUpActivity you entered to UserProfileActivity. (Finishing the SignUpActivity i.e. SignUpActivity is removed from back stack).
Now you're in UserProfileActivity and pressing back button from there takes you to LoginActivity because SignUpActivity was already removed from back stack.
But you said, you're in PartyListActivity which can happen only if you start it programmatically from LoginActivity.
Look in your LoginActivity , the unexpected activity is starting from there.
More about Tasks and Back Stack.
You can specify a "parentActivityName" attribute in the definition of an activity in AndroidManifest.xml. Do you have such an attribute in the definition of your SignUpActivity?
Full documentation of this attribute and other information around back stack navigation is available at https://developer.android.com/training/implementing-navigation/temporal.html.
Option 1 : It doesn't make sense that PartyListActivity is launching after UserProfileActivity is finished. Somewhere somehow you configured or calling to that activity. Double check your codes.
Option 2 : try to finish UserProfileActivity activity itself onBackPress() (even it will finish)
#Override
public void onBackPressed() {
super.onBackPressed();
finish();
}

Android onCreate not firing on new activity

Hi there I am trying to execute some code when I change to the next activity, but it does not seem to work. The Previous activity is a login page if the user is already logged in it goes straight to the new activity. But the onCreate does not seem to fire.
Main Activity
public class MainActivity extends AppCompatActivity {
private View mMainView;
private Meteor mMeteor;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTitle("ACTIVITY");
Log.d("SimpleActivity","OnCreate Started");
if(MeteorSingleton.getInstance().isConnected()){
Log.d("Connection", "Connected");
}else{
Log.d("Connection", "Not Connected");
}
}
........
The strange thing is the setTitle works but none of the logs.
Here is some code from the previous login in page.
#Override
public void onConnect(boolean signedInAutomatically) {
Log.i("Connection", "Connected to host server");
if (mMeteor.isLoggedIn()) {
openMainScreen(mLoginFormView);
}
}
public void openMainScreen(View view) {
Intent dashboard = new Intent(getApplicationContext(), MainActivity.class);
startActivity(dashboard);
}
Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="system.carproject.adam.ams">
<!-- To auto-complete the email text field in the login form with the user's emails -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<!--
The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
Google Maps Android API v2, but you must specify either coarse or fine
location permissions for the 'MyLocation' functionality.
-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".LoginActivity"
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=".MainActivity"
android:label="Activity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="#string/google_maps_key" />
<activity
android:name=".MapsActivity"
android:label="#string/title_activity_maps"></activity>
</application>
</manifest>
If someone could educate me on this would be great. Can seem to figure it out.
Thanks
You have created two LAUNCHER Activities and that's created two App Icon in your Device so if you think its open directly MainActvity then its possible if you click on second app icon in your device for same application . check your device .
first remove LAUNCHER mode from MainActvity in Android Manifest and then you have to add manual check in your login Activity onCreate() for login status and then startActvity() MainActivty if login status is true.
try removing the intent filter tag from activity tag of MainActivity in Manifest:-
<activity android:name=".MainActivity"
android:label="Activity"> </activity>

Launcher Activity is opening twice in my new SDK

I am recently facing a problem while creating new android application in both eclipse as well as android studio.
I am using same SDK in both eclipse and android studio.
When I create a new application and i simply run it. The launcher activity was getting loaded twice.
Means, I am getting MainActivity loading twice one on top of another.
OnCreate() method of my MainActivity also got invoked twice.
The code goes like follows
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sample"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="21"
android:targetSdkVersion="22" />
<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>
</application>
MainActivity.java
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
System.out.println("OnCreate Invoked");
}
}
Output
OnCreate Invoked
OnCreate Invoked
Can you please help me in resolving this issue.
Thanks in advance.
It seems you are getting multiple instance of your first activity.
Use this in manifest of 1st activity:
android:launchMode="singleTop"
If I set orientation programatically in onCreate - I can reproduce this issue.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
If needed, set activity orientation in the manifest file like this.
<activity
android:name=".MainActivity"
android:screenOrientation="landscape">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

How does an android program understands which class or/and activity to call first?

As i am new to android programming, I don't know how to call a certain class and/or certain activity first.
For example i have two classes viz. 1) Login.java 2) Create.java and two xml files associated with them are activity_main.xml and create_new.xml respectively.
So how can i make Login.java run first with activity_main.xml as screen?
in login java use this
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);}
in create java us this
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.create_new);
in androidmanifest
<activity
android:name=".Login"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
and another activities
<activity android:name=".MainActivity" ></activity>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.your.package"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.your.package.activity.Login"
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.example.your.package.activity.Create"
android:label="#string/app_name" >
</activity>
</application>
</manifest>
In this case, the intent-filter of LOGIN activity specifies that the intent with action of MAIN and category of LAUNCHER will be caught by it, aka it is where the application starts.
Afterwards,
public class Login extends Activity
{
//honestly I'd name this class LoginActivity and same in the XML
#Override
public void onCreate(Bundle saveInstanceState)
{
super.onCreate(saveInstanceState);
setContentView(R.layout.activity_main);
}
....
}
Also look at this example to learn how to use Fragments:
NullPointerException accessing views in onCreate()
from the intent filters you decalre in the android manifest file of your project;see the image
You declare an intent filter in the activity tag something like shown in the image for making it the first activity of your application.and if there are other activities in your app having intent filters than you have to just change the capital letter MAIN to DEFAULT in the intent filter tag
You will find AndroidManifest file like in the following image in your project

Is it possible to have an Activity over a NativeActivity and still getting inputs in the NativeActivity?

I have a NativeActivity which works by itself and I try to add some SDKs ( more precisely Admob/Chartboost/Facebook ).
On the other hand I've got custom scripts which manage all the SDK only if we pass an Activity in the parameters.
My goal is to manage all of the SDKs with a custom Activity over my NativeActivity.
My questions :
Is It even possible ?
Is there a simpler solution than mine ?
So far my inputs are stucked in the Activity.
Here's my NDK class ( from the android-ndk examples ) :
public class TeapotNativeActivity extends NativeActivity {
// Some previous code
public void showUI()
{
// Some previous code
ASE_GUIActivity.pNatAct = this;
Intent startNewActivityOpen = new Intent(this, ASE_GUIActivity.class);
this.startActivity(startNewActivityOpen);
}
}
Here's my activity class :
public class ASE_GUIActivity extends Activity {
public static NativeActivity pNatAct;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public boolean onTouchEvent(MotionEvent ev) {
pNatAct.onTouchEvent(ev);
return super.onTouchEvent(ev);
}
}
And finally my AndroidManifest.xml :
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sample.teapot"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="17" />
<uses-feature android:glEsVersion="0x00020000"></uses-feature>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme"
android:hasCode="true"
android:name="com.sample.teapot.TeapotApplication"
>
<!-- Our activity is the built-in NativeActivity framework class.
This will take care of integrating with our NDK code. -->
<activity android:name="com.sample.teapot.TeapotNativeActivity"
android:label="#string/app_name"
android:configChanges="orientation|keyboardHidden">
<!-- Tell NativeActivity the name of or .so -->
<meta-data android:name="android.app.lib_name"
android:value="TeapotNativeActivity" />
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.littleworlds.ase.ASE_GUIActivity"
android:theme="#android:style/Theme.Translucent"
android:screenOrientation="landscape"
android:configChanges="keyboard|keyboardHidden|orientation"/>
</application>
</manifest>
As far as I know, you can not instantiate an activity inside an activity. You can only use intent mechanism, in which case you will only have one activity running at the same time. Your design choice does not seem suitable for Android environment.
Why don't you try to launch the SDK activities you mention from native side? For example, if you want to launch a Facebook login screen when user clicks a button, get the necessary input inside native activity and then using JNI calls and intent mechanism, launch the Facebook activity.

Categories

Resources