I want to implement following approach when i move back and forth while traversing activities
MainActivity is the entry point activity. Menu inside this activity opens the PreferenceActivity
PreferenceActivity is and activity which shows preferences/settings and clicking it over one of
the preferences will call HelpActivity
HelpActivity contains the help of application.
*Note : once i reach to MainApplication and if i press back i want to go out of the application.
i tried calling finish() after every intent i call but that ruled out the CASE #2
update
When i reach to HelpActivity in CASE #1 and press a button i want to go to the MainActivity and all the other Activities should wiped out
Use this scenario. And let me know what happen.. (Only pseudo code actual may be different)
MainActivity:
1. startActivityForResult(PreferenceActivity);
2. onBackPressed()
{
finish();
}
PreferenceActivity:
1. startActivityForResult(HelpActivity);
2. onActivityResult()
{
finish();
}
3. onBackPressed()
{
finish();
}
HelpActivity:
1. onBackPressed()
{
finish();
}
case #1:
MainActivity :
public class MainActivity extends Activity implements View.OnClickListener {
button add;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.youeLayout);
add = (Button) findViewById(R.id.buttonIdName);
add.setOnClickListener(R.id.buttonIdName)
public void onClick(View v) {
// TODO Auto-generated method stub
Intent data = new Intent(MainActivity.this,PreferenceActivity.class);
startActivity(data);
}
and the same code for the PreferenceActivity just change the onClick() method to the appropriate classes you want
in the helpActivity:
add the onBackPressed() method like this:
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
Intent youeIntentName = new Intent(HelpActivity.this,MainActivity.class);
startActivity(youeIntentName);
}
case# 2 :
you don't need to change any thing when the user press back after he go from MainActivity to PreferenceActivity he will be back the MAinActivity and so on
Related
i am working on an app in which, button Onclick dynamically created a button from one activity and button is appeared in another activity.
So, for your button, see the code below. I'm just using SharedPreferences for testing if button has been clicked before or not.
// Inside your onCreate() method of your SecondActivity.java
((Button)findViewById(R.id.activity2_button)).setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
getSharedPreferences("SharedPreferences", Context.MODE_PRIVATE).edit().putBoolean("ShowButton", true).commit(); // put Boolean inside SharedPreferences
Intent main = new Intent(this, FirstActivity.class);
main.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(main);
finish();
}
}
And now, the FirstActivity.java code :
public class FirstActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity1_layout);
if (getSharedPreferences("SharedPreferences", Context.MODE_PRIVATE).getBoolean("ShowButton", false))
((Button)findViewById(R.id.activity1_button)).setVisibility(View.VISIBLE);
else
((Button)findViewById(R.id.activity1_button)).setVisibility(View.GONE);
}
}
Test it, and tell me if this works great. Hope it will work for you, Darkball60 :)
I want to navigate to another screen when user touch back button. I found a method for this.
#Override
public void onBackPressed() {
// do something on back.
return;
}
I'm navigating from a fragment.But it is not clear that where I have to use this and how. Please help.
You'll need to add that method to the Activity class of the screen you're navigating from.
Then, in that method, add your intent code to move to your second Activity (let's call that Activity2 below) using startActivity
#Override
public void onBackPressed() {
Intent intent = new Intent();
intent.setClass(this, Activity2.class);
startActivity(intent);
}
Just override the method onBackPressed and navigate to the screen you want to via intents or whichever way you prefer.
#Override
public void onBackPressed() {
getFragmentManager().beginTransaction.replace(thisfragment,new Fragment).commit();
}
The above code will replace your current fragment with the new fragment to which you want to navigate.
Am creating small application using login, register and user details. After login am going to store login credential to shared preference and navigate to dashboard activity. In second time directly navigate to dashboard activity. This level of code is working fine.
Please consider I have three activity MainActivity, LoginActivity, RegisterActivity, DashboardActivity and ProfileActivity.
In my MainActivity If user value is sharedPreference directly moving to DashboardActivity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(new SessionManager(getApplicationContext()).isLoggedIn()){
startActivity(new Intent(getApplicationContext(), DashboardActivity.class));
}
}
In my DashboardActivity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ImageButton imgBtn = (ImageButton) findViewById(R.id.imageButton1);
imgBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(getApplicationContext(), ProfileActivity.class));
}
});
}
Now am simply press back button in device goes to MainActivity not DashboardActivity. I want to move activity to DashboardActivity only, not MainActivity. Please guide me how to do that. And how to handle session in Android.
Also I have bit confusion in using which flags and where to use. I tried in DashboardActivity but not working.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ImageButton imgBtn = (ImageButton) findViewById(R.id.imageButton1);
imgBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent newIntent = new Intent(getApplicationContext(), ProfileActivity.class);
newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
newIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(newIntent);
}
});
}
Now I tried various possible ways but not getting solution.
Main problem occurs after navigating DashboardActivity to any Activity won't come back to DashbarodActivity instead of going MainActivity. I don't know why it happens. Any problem in MainActivity navigation
I'm not sure about that, but in DashboardActivity try to start your activity like this: startActivity(new Intent(DashboardActivity.this, ProfileActivity.class));
Just finish the current activity after starting the new one via Intent and you should achieve the desired functionality and this way you don't need any flags. Except for the MainActivity of course.
Just simply override onBackPressed method in your ProfileActivity and call finish method.
#Override
public void onBackPressed() {
finish();
}
EDITED ANSWER:
on your ProfileActivity, try to override onBackPressed method then start the DashboardActivity again and don't forget to finish the current activity.
#Override
public void onBackPressed(){
startActivity( new Intent(this, DashboardActivity.class) );
finish();
}
I have two classes in question. Both extend Activity.
Class A
public void displayinfo()
{
setContentView(R.layout.dynamicinfo);
//Add some buttons dynamically here
//do some processing
// move on to Class B
}
In Class B: I want to go back to Class A state in UI if BACK button is pressed.
Class B
//Register a listener for this button
Backbutton.setOnClickListener(new OnClickListener()
{
public void onClick(View arg0)
{
Log.i("setOnClickListener", "Pressed Back Button ");
Toast.makeText(mycontext, "Pressed Back Button", Toast.LENGTH_SHORT).show();
//HERE I want to go back class's function in UI as well as restoring the sttae for that screen.
}
how do I do that?
I looked around some questions.
they did not answer clearly what I am looking for.hence the posting.
thanks.I think I was adding my own Back button on the Layout I created in the Class B's UI Screen --not using the regular "Back" button on the key board. May be that was the problem.
If both class A and class B are activities, and class B activity is started from class A activity, then you should only finish() you class B activity then you should return to class A with its state preserved.
I am not sure if you are asking this as this seems a very basic android activity flow.
Class A :
public void displayinfo()
{
setContentView(R.layout.dynamicinfo);
//Add some buttons dynamically here
//do some processing
// move on to Class B
/*For starting activity B use this code*/
Intent in=new Intent(this,CalssB.class);
startActivity(in);
}
now in class B you just need to finish activity B code :
Backbutton.setOnClickListener(new OnClickListener()
{
public void onClick(View arg0)
{
Log.i("setOnClickListener", "Pressed Back Button ");
Toast.makeText(mycontext, "Pressed Back Button", Toast.LENGTH_SHORT).show();
/* This will finish current activity B and back to activity A with same state.*/
finish();
}
refer this link for understand in details.
http://developer.android.com/guide/topics/fundamentals/tasks-and-back-stack.html
The following code helps you to navigate your activity from Class B to Class A when back button is pressed.
ClassA.java
/***********/
startActivity(new Intent(ClassA.this,CalssB.class);
ClassB.java
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if(keyCode == KeyEvent.KEYCODE_BACK)
{
finish();
//startActivity(new Intent(ClassB.this,ClassA.class));
Toast.makeText(getApplicationContext(), "Backbutton pressed", 30).show();
}
return super.onKeyDown(keyCode, event);
}
This code is used of for back button....
If in button click you need to navigate page to ClassA means use anyone of the below code..
ClassB.java
btn.onClick(){
finish();
(or)
startActivity(new Intent(ClassB.this,ClassA.class));
}
How exactly do you move to ClassB ? You can move to the ClassB with an Intent.
public void displayinfo()
{
setContentView(R.layout.dynamicinfo);
//move on to Class B like this:
Intent k = new Intent(this, ClassA.class);
startActivity(k);
}
Now when you have moved to ClassB, and the Back button is pressed it will automatically move to ClassA
Finally I figured out what was wrong.
From each activity I was calling a finish() before invoking the next activity via Intent mechanism.
And hence when the Back button was pressed from different activities, there was nothing to go to.
And hence the APP used to crash.
Thanks for all kind responses.
Much appreciated.
As always, learnt something new from each of the response.
I fixed it by removing the finish() in the activities that need to be present.
I didnt have to re-do or re-build the UI (even the dynamic buttons). :-)
in class b add
function addReturn(ClassA back){
// back.take over the control
}
i would use an interfase or extension if you want to be flexible for your viewports.
in Class a before pass control over you need to add ’this’ to the b class.
In my application I have an activity class A that has a listview with a cursor adapter.
From A I can go to the activity B, by pressing a button. From B I can go back to A by pressing a button (not by pressing the BACK button). This means that a new instance of the A activity is created.
From this point, if I press the BACK key, the current A activity is destroyed and B is popped. And if I press BACk again the initial A activity is popped. I hope it is clear.
My problem is that when the second A activity is destroyed, the database connection is reseted, in a static manner. So in the end, when the initial A activity is displayed, the listview will be empty.
My question is: should I try to have a single instance for the A activities, or shoud I change the database connection (to link it with the activity instance)?
Thanks a lot
Gratzi
First Of All In class A which is carrying your ListView . on clicking any Listview call the startActivity method for the Class B Activity without calling any finish().
I hope which is you are already doing.
Now in the Second Activity The button (Not the Back Button) you are using for calling Activity A . in its clickListener for calling Activity A dont call the startActivity(intentForA) instead call the finish(); for ending the Activity B. this will resume the A activity which is paused..
I hope this will help
You will need to create 3 Activities rather than 2.
Have a MAIN activity that does not really display anything.
So You have Activity A that is your main activity that can handle the connection to the DB etc.
Then Activity B and C can be the A and B that you have used.
Activity A (Main activity) can have a static instance of itself so you can refernce it's
Variables etc -OR- you can pass data from one activity to the other using Intent.put, etc.
I prefer the global static instance way as I'm a little old school on Java.
Edit:
Forgot to mention, to handle the 'closing' of the app, either Activity B or C must also close Activity.
public class ActivityA extends Activity {
ActivityA act_a_instance;
public int some_integer = 22;
#Override
public void onCreate(Bundle savedInstanceState) {
act_a_instance = this;//Now you can reference this Activity outside
//Your creation stuff etc
}
}
public class ActivityB extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
//Your creation stuff etc
//Reference stuff from ActivityA like so :
int temp_integer = ActivityA.act_a_instance.some_integer;
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.options_back:
startActivity(new Intent(this, ActivityC.class));
break;
}
}
#Override
protected void onStop() {
finish();
super.onStop();
}
}
public class ActivityB extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
//Your creation stuff etc
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.options_back:
startActivity(new Intent(this, ActivityB.class));
break;
}
}
#Override
protected void onStop() {
finish();
super.onStop();
}
}
Use below code hope this will solve your problem
Intent i = new Intent(B.this, A.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);