how do i call methods from another class in android? - android

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.

Related

Setting a another view to load when touch back(hard key) in android

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.

How to go previous Activity by using back button

I have created two activity i.e Activity A & Activity B ,if i clicked Next Button i.e in Activity A going to Activity B properly But when i click on back button i want to go from Activity B to Activity A and page swipe from left side to right side and on click next page swipe right to left,
here is my code
public void onBackPressed() {
Intent intent = new Intent(ActivityB.this, Activity.class);
startActivity(intent);
finish();
Just Remove finish() from Activity.
Because when you go to second activity and finish first activity, there is no any activity and stack.
So If you click back button from second activity, application will be finish if there is no Activity in stack.
You should use this Approach.
Ex.
In Activity.java
Intent first = new Intent(Activity.this,ActivityB.class);
startAcivity(first);
// Don't use finish() here.
In ActivityB.Java
Just click on built in back button.
or If you want to use your own back button.
Use finish(); in button click event.
You can use only onBackPressed();
public void onBackPressed() {
super.onBackPressed();
}
Android Overriding onBackPressed()
How to go previous Activity by using back button
No need to put an intent and start a new activity that would take you to previous activity.
Just call 'finish()'
It would go back to previous activity as Android activities are stored in the activity stack
If you have other activities that are present in between the activites say if android stack is filled with Activity A>Activity C>Activity B,If you want to go to Activity A on finish of Activityy B then you have to set an intent flag like FLAG_ACTIVITY_REORDER_TO_FRONT or FLAG_ACTIVITY_PREVIOUS_IS_TOP
Just use finish() no need for intent as A is already in stack and when you finish B, A will come to surface
public void onBackPressed() {
finish();
}
Read this to learn more about android activity stack.
public void onBackPressed() {
// TODO Auto-generated method stub
finish();
super.onBackPressed();
}

manage activity back and forth

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

How to go back from second screen to first screen

How to switch layouts? First, I have a class Main where is onCreate (setContentView(R.layout.main);) and then I call, another class with command:
setContentView(secondClass);
In this class, I draw with Canvas and this work just fine. I also create button to go back in first "class" (R.layout.main), but I don't know how to do it.
Now my program is basic a graph shower. In first class you type your function and them second class draw it. But how to go back in first class to type another function. This "back" button or arrow witch every Android phone have, send me out of program not back on insert part.
In secondClass I can't create onCreate method, but I also tried the following and they didn't work:
Intent abc = new Intent("bla.bla.bla.FIRSTCLASS");
startActivity(abc);
and
Intent abc = new Intent(SecondClass.this,FirstClass.class);
startActivity(greNaPrvoOkno);
If you want to use a custom view (as I understood, you are extending the View class), you can do it in the following way;
Consider you are showing the second class from your Main activity like this;
setContentView(new SecondClass(getApplicationContext(), MainActivity.this));
And you Second class is this (suppose);
// I am using onClickListener to go back to main view. You do whatever you like.
public class SecondClass extends View implements OnClickListener {
// This is needed to switch back to the parent activity
private Activity mParentActivity = null;
public SecondClass(Context context, Activity parentActivity) {
super(context);
mParentActivity = parentActivity;
setOnClickListener(this);
}
#Override
public void onClick(View v) {
// Set the Main view back here.
mParentActivity.setContentView(R.layout.main);
}
}
Disclaimer: This code will do what you have asked for, but may cause other problems.
As advised by #Mudassir, you should use two different activities for two screens. It will give you better control, and your code will be easy to understand and maintain.
On the Onclick event of the button you have to write finish(); that's it..
Both of your classes are Activities yes? IF so then in your second activity you will simply call finish() and your activity will close revealing your first activity again.
When I have used multiple intents in my android application, I have created a new activity through:
Intent abc = new Intent(this, SecondClass.class);
startActivity(abc);
When the button is pressed in your second class, I would then either call finish(); on the class, or create a new intent like so:
Intent abc = new Intent(this, FirstClass.class);
startActivity(abc);
However, this method has the disadvantage that if a user wanted to use the back button, they may have to scroll through many layers of activities.
You should create another activity for your second class but not just set the main activity to a new view setContentView(secondClass).
For an easier modification, You could try to set the view back to setContentView(R.layout.main) first.
You still need to configure the widgets(e.g. TextView) on it when you set it back.
You don't have to startActivity again to go back.
Just call finish() in your second activity when you want to finish the current activity and go back:
e.g. When user press the back button in your second activity
mButtonBack.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
finish();
}
}

Multiple activity instances problem

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);

Categories

Resources