I'm having a problem with an Activity being rotated after I get back from another that has a different screen orientation.
Allow me describe the steps to reproduce this behavior:
1.I have an Activity declared in the Manifest like this:
<activity
android:name=".JobActivity"
android:label="#string/title_activity_job"
android:screenOrientation="portrait" />
As you can see, screenOrientation is set to portrait
Inside this Activity I launch another Activity with the following Intent:
Intent jobDetailsIntent = new Intent(mActivity, JobDetailActivity.class);
startActivity(cameraIntent);
This Activity screen orientation is declared to be landscape.
<activity
android:name=".JobDetailActivity"
android:screenOrientation="landscape"
android:theme="#style/ThemeFullscreen" />
The Activity JobDetailActivity when pressing a button, start another Activity waiting for a result.
startActivityForResult(getIntent(activity), requestCode);
This other Activity is declared as portrait
<activity
android:name=".QuestionsActivity"
android:screenOrientation="portrait"
android:theme="#style/ThemeFullscreen.Color" />
When I get the result from this other Activity, and after doing a couple of things, I call finish in order to return to the first Activity (JobActivity).
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
// Some other stuff
this.finish();
}
}
At this point, when I get back to JobActivity I get to see the Activity in landscape (Remember it was declared as portrait) for a second and then returns to the original position.
To sum up:
A (portrait) -> B (landscape) -> C (portrait)
After receiving result from C and going back to B
A (portrait) -> B (landscape)
After calling finish from B
A (landscape)
After a second
A (portrait)
Any ideas why this may be happening? Thank you all, your help is much appreciated.
I don't understand this code:
Intent jobDetailsIntent = new Intent(mActivity, JobDetailActivity.class);
startActivity(cameraIntent);
There is a difference between the name of intent declare and intent used to start the activity.
Maybe the problem is here.
Thank you.
Related
i`am new to unit testing and i have this task to write unit testing for Post API i have only the end point and api body i read a lot in google but i could not figure out how to start , any help ?
You must create a intent, fill it with data if needed, like the data extracted from the QR code and then call startActivity.
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
Bundle b = new Bundle();
b.putInt("key", 1); //Your id
intent.putExtras(b); //Put your id to your next Intent
startActivity(intent);
finish();
I put an int in the bundle but it could be anything that implements Serializable.
You can reach direct on required screen with that pages’s activity (as shown Splash screen in below example). You can launch activity with below method.
public static void launchActivity(Activity activityName)
{
((AndroidDriver<MobileElement>) driver).startActivity(activityName);
}
How you can call this function
Suppose you have below app package and activity (its for example but you have to use for your app)
String appPackage ="my.app.helloworld";
String appActivity = "my.app.helloworld".common.activity.SplashScreen";
launchActivity(new Activity(appPackage, appActivity));
You need to set android:exported="true" in your AndroidManifest.xml file to resolve error java.lang.SecurityException
<activity
android:name="com.dsquares.lucky/.screens.mainscreens.Wallet.WalletPayment.AddFundsActivity"
android:label="wallet"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" >
</action>
</intent-filter>
</activity>
This question already has answers here:
How to start new activity on button click
(28 answers)
Closed 7 years ago.
I want to start a new activity when i press the button, but when i press it my app crashes!
Where is the Problem?
Here is the code!
public void onClickButtonListener() {
button_play = (Button)findViewById(R.id.play_button);
button_play.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(".SecPage");
startActivity(intent);
}
}
);
}
Your Intent should have two parameters. The current activity and the activity it is going to.
Intent intent = new Intent(this, SecPage.class);
startActivity(intent);
Please consider the following issues,
Make sure you have specified your source and destination classes,
Intent intent = new Intent(ActivityA.this, ActivityB.class);
startActivity(intent);
Make sure you have added an activity tag in the manifest file,
<activity
android:name=".ActivityA"
android:theme="#style/FullscreenTheme" >
</activity>
If you are using the Activity class in a different package add the full package name,
<activity
android:name="com.silverlining.bionot.ActivityA"
android:theme="#style/FullscreenTheme" >
</activity>
You are probably trying to invoke a custom action with this constructor for Intent:
public Intent (String action)
Make sure you are using the full custom action string, something like com.google.app.myapp.SecPage.
According to android documentation,
public Intent (String action)
Create an intent with a given action. All other fields (data, type,
class) are null. Note that the action must be in a namespace because
Intents are used globally in the system -- for example the system VIEW
action is android.intent.action.VIEW; an application's custom action
would be something like com.google.app.myapp.CUSTOM_ACTION.
Okey, this is weird.
I have two activities (well, more than two, but they don't matter) - AppActivity and PopupActivity. AppActivity is my main application activity and it contains my app's settings. PopupActivity is a dialog that gets opened when user clicks the button in the notification (it's a RemoteViews notification). Well, it works. Works great. But only if user closed the AppActivity by clicking the back button. If they clicked home, PopupActivity opens after a few seconds after clicking the button. Same thing happens when user closes the PopupActivity with a home button. Clicking the button in the notification doesn't open the PopupActivity instantly, but it takes a few seconds to kill the previous activity that still exists somewhere in the background.
I've tried calling finish() in the onStop and onPause methods, but it doesn't fix my problem.
Edit: Here's the code I have:
Manifest:
<activity
android:name="cc.lupine.quicksocial.AppActivity"
android:label="#string/app_name"
android:configChanges="orientation|screenSize" android:noHistory="true" android:clearTaskOnLaunch="true" android:finishOnTaskLaunch="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:configChanges="orientation|screenSize"
android:excludeFromRecents="true"
android:showOnLockScreen="false"
android:theme="#android:style/Theme.Holo.Light.Dialog.NoActionBar.MinWidth"
android:name="cc.lupine.quicksocial.PopupActivity"
android:noHistory="true"
android:launchMode="singleInstance"
android:taskAffinity=""
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
<service android:name="cc.lupine.quicksocial.ShareService"></service>
ShareService.java (just a function that gets called when user clicks a button in the notification):
public static void startSharing(Context ctx, int n) {
Log.d("sn", "startsharing called in shareservice");
if(n == 1 || n == 2)
{
Intent i = new Intent(ThisApplication.getAppContext(), PopupActivity.class);
i.putExtra("shareType", n);
i.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY|
Intent.FLAG_ACTIVITY_CLEAR_TOP|
Intent.FLAG_ACTIVITY_SINGLE_TOP|
Intent.FLAG_ACTIVITY_CLEAR_TASK|
Intent.FLAG_FROM_BACKGROUND|
Intent.FLAG_ACTIVITY_NEW_TASK);
ctx.startActivity(i);
} else if(n == 3) {
// doesn't matter for now
}
}
PopupActivity.java (fragments):
public class PopupActivity extends Activity implements OnDataPass {
#Override
protected void onCreate(Bundle savedInstanceState) {
Log.d("sn", "oncreate called in popupactivity");
super.onCreate(savedInstanceState);
instanceOfPopupActivity = this;
setContentView(R.layout.activity_popup);
ShareFragment sfrag = new ShareFragment();
Bundle args = new Bundle();
Bundle extras = getIntent().getExtras();
try {
//doesn't matter
} catch(Exception e) { e.printStackTrace(); finish(); }
sfrag.setArguments(args);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.contFragment, sfrag);
fragmentTransaction.commit();
}
#Override
public void onPause()
{
Log.d("sn", "onpause called in popupactivity");
finish();
super.onPause();
}
#Override
public void onStop() {
Log.d("sn", "onstop called in popupactivity");
super.onStop();
}
#Override
public void onDestroy()
{
Log.d("sn", "ondestroy called in popupactivity");
super.onDestroy();
}
}
And if I open the popup for the first time:
05-26 14:37:14.149: D/sn(7218): startsharing called in shareservice
05-26 14:37:14.179: D/sn(7218): oncreate called in popupactivity
But when I close the popup with a home button and try to open it again:
05-26 14:38:11.620: D/sn(7218): startsharing called in shareservice
05-26 14:38:14.103: D/sn(7218): oncreate called in popupactivity
It takes a lot of time for onCreate to be called. And now, what's the reason?
I think you are focusing on the wrong problem (killing the activity) instead of focusing on the real problem (10 seconds to start it again).
First you need to understand WHY it is taking 10 seconds to open it if you exited the other acitivty with the home key!
If you posted more details (with source code) it would have been easier to understand and help you!
call finish() when you want to close app. Call finish() on both activities and watch out popup window has to be focusable to handle click.
Assume i have activity A that act as the root activity for my app . and form this activity i go to activity B.
I want to be able to go back from B to A Without creating new instance of Activity A.
this code is in Activity B
public void onBackPressed() {
super.onBackPressed();
// Intent intent= new Intent(getBaseContext(), MainActivity.class);
// intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
Intent myIntent = new Intent(getBaseContext(), MainActivity.class);
myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(myIntent);
Log.d("Back", "TEST");
}
but it sill call the onCreate on activity A . What i want to do is have A in the background when activity b is started and whenever it is finished switch back to activity A
this is the manifest
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main"
android:screenOrientation="unspecified"
android:launchMode="singleTask"
android:stateNotNeeded="false">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME"/>
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:label="#string/app_name"
android:name=".SubmenuActivty" >
</activity>
According to android activity life cycle, when you launch an activity A :
Life Cycle method list :
Activity A.onResume();
Activity A.onStart();
Activity A.onCreate();
Activity Status :
Activity A : Resumed
When you launch the activity B now :
Life Cycle method list :
Activity A.onStop();
Activity B.onResume();
Activity B.onStart();
Activity B.onCreate();
Activity A.onPause();
...
...
...
Activity Status :
Activity A : Stopped
Activity B : Resumed
And when you again start A now :
Life Cycle method list :
Activity B.onDestroy();
Activity B.onStop();
Activity A.onResume();
....
....
....
Activity Status :
Activity B : Destroyed
Activity A : Resumed
This is the life cycle of an activity :
You can find the details here
According to default behavior, activity A goes to onStop() state and doesn't alive and need to create a new instance on coming back to activity A. All I know - there is no way to keep alive of the A instance.
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
Try it... It makes old activity to front without new instance...
You Dont need do anything. Just remove your onBackPressed() method from Activity B.
By default when you move from Activity A to Activity B, Android adds the Activity A to the backstack. When you press the back button from Activity B or finish it, it automatically restore the Activity A from backstack.
If you wish to finish your Activity B programmatically call activity's finish() method when you want to do it.
Try this
public void onBackPressed() {
super.onBackPressed();
Log.d("Back", "TEST");
}
And thats all, what you need.
Use moveTaskToBack(true);
public void onBackPressed() {
// TODO Auto-generated method stub
moveTaskToBack(true);
super.onBackPressed();
}
or You can also use finish()
Why you start an activity in onBackPressed anyway?
You should super.onBackPressed(); instead of starting new activity. Or call finish() method in you onBackPressed() method:
public void onBackPressed() {
finish();
Log.d("Back", "TEST");
}
For my news-reader app, I made a widget. Using the widget it is possible to start the news-app. But if the user goes back to the home screen, with the back button, there still is an instance of the application. So if the user goes the applications-list (all the app's) and start the news-app again. There are 2 instances of the news app :S For closing the app, the users needs to push 2 times on the back button (because you see 2 times the same app). Can I do anything about it?
Here my code:
static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId) {
Intent configIntent = new Intent(context, Main.class);
PendingIntent configPendingIntent = PendingIntent.getActivity(context, 0, configIntent, 0);
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
remoteViews.setOnClickPendingIntent(R.id.headlinesBox, configPendingIntent);
// Tell the widget manager
appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
}
It seems that you have to add to the manifest:
<activity android:name=".Main"
android:launchMode="singleInstance">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
</intent-filter>
</activity>
You can set true on android:clearTaskOnLaunch activity attribute in the Manifest file.
It should always close all the extra Activities and start the App from the first Activity.