How to auto press a button , when a activity is started - android

My main activity have 2 buttons,say A and B
I always press A, and go to 2nd activity
Please tell how i can do this automatically, like when i open my app, main activity is on, button A pressed programatically, and what user see is activity2 ,when opening the app
PLEASE DONT TELL ME TO REMOVE BUTTON B,or USE 2nd activity only
Beacause some background threads are needed to be runned on main activity,

Write below code at the end of oncreate() of mainactivity... this will surely help you
A.performClick();

buttonA.performClick();
Add this code to your main Activity

Related

How should I manage the activity stack?

I have an app with a main menu at bottom; I can't figure out how should I manage the activity stack, because every button opens an activity, and each activity can start more activities, and i was looking for a management in the style of the current Instagram's app. It looks like (in the Instagram app) that every activity started by each button in the bottom menu opens a new activity stack, but when you press back button, it navigates in the reverse order you called every activity.
Sorry for my bad explanation, i hope that you can understand my aim.
You could check out Instagram Android app to figure out what is my goal.
My current implementation uses a MainActivity with a Fragment for the first menu button (Qui in giro->"Nearby"), but i probably should change this approach.
Thanks.
1) Firstly you will have to decide what will be your main activity.
2) Now use this main activity like as star topology. Means this main activity will be center activity.
3) Use to finish() method for finish back stack before receiving at this center activity.
4) After it if you open another side of your main activity than you will see only new back stack will be displayed after pressing back button for receiving at main activity again.

How to prevent reload activity in android

Have Activity A ,here am retrieving data from server .On click of button am finishing current activity and start B activity ,on click of button again i want to go A activity by finishing B activity but i dont want to reload A activity
You should have a look at the lifecyle of an activity and the activity stack. developer.android.com/guide/components/tasks-and-back-stack.html. To know more about the this developer.android.com/training/basics/activity-lifecycle/index.html. Go through the links and you should able to solve your problem.

Why my android application is exiting when I press the Back key

I am developing a small application at the moment it consists on 3 Activities.
Now when I start my application it starts fine and I can navigate from activity1 to activity 3 properly and without any problem.
Activity1-->Activity2--->Activity3
The problem comes that when I press the back button of my mobile device to go back to activity2, the application simply closes.
Can somebody please suggest how to figure it out what is happening.
that is how i am going to Activity2 from Activity2
Intent activity3 = new Intent(Activity2.this,Activity3.class);
Activity2.this.startActivity(activity3);
Activity2.this.finish();
Note:I am not using emulator I am using mobile and doing all the debugging directly on the mobile.
Thanks
This is because you are calling the finish() function which removes the activity from the stack. Remove the line Activity2.this.finish(); and you should be going back the way you wanted.
Because you call finish() method.
Due to this from activity stack your last activity is removed so that's way your current activity is finished on backPressed().
To overcome this You must remove
Activity2.this.finish();
from your code.

Android Robotium: how to go back to my Activity under test after clicking/launching another Activity

I have an issue with my Robotium tests. On one of my Activity A, I am clicking on a button. Clicking on this button launches another activity B. So in my robotium test I have something like this :
Button myBtn = (Button) solo.getView(R.id.myBtn);
so after this action, the emulator is automatically launching activity B. Now, the problem is that I do not have any possibility of going back to the activity under test (A) in the code.
Can you please tell me how to avoid getting jailed in the activity B when clicking on the button that trigger its launch ? In other words, is it possible to go back to the activity under test ?
FYI : I need to go back to the activity under test because there are other test methods waiting to be fired.
thanks in advance,
Please try methods like
solo.clickOnView(R.id.myBtn)
to click on your button
You can also try
solo.clickOnButton()
To go back to activity you can use
solo.goBack();
or
solo.goBackToActivity("ActivityName");
Once you call any activity outside your application, it’s not possible to come back to your application using solo.<any API>, because solo is tight to UID of your application and doesn't work on other app's activity (with different UID).
Basically solo can only work on activities which belong to the application for which it’s created.

Is there a way to write code to go to the previous activity?

Right now in my android app a user presses a button to go to another activity, and then must press the back button on android to return to the previous activity. Can I have a button on my app and write code to go to the previous activity?
As the easiest way, you code write a code where your current activity is closed on a particular event, say on a button press. the method to be called is finish()
This way, when your current activity is finished, you are taken back to your previous activity.
Yes Ofcourse. : See the vogella article

Categories

Resources