I am implementing Tab environment as follow :
There is one class TabScreenABCActivity extends TabActivity,in that i have handled the tabchange functionality.
I have implemented MyTabGroupActivity extends TabGroupActivity. From this class, i have called MyActivity like so:
startChildActivity(getResources().getString(R.string.MyActivity), new Intent(this,MyActivity.class));
Now, MyActivity extends TabGroupActivity, from this class i called two other classes using intent like so:
Intent intent=new Intent();
intent.setClass(MyActivity.this,XYZActivity.class);
TabGroupActivity tab = (TabGroupActivity) MyActivity.this.getParent();
tab.startChildActivity("Tab", intent);
I have also overridden method to go back Activity onBackPressed() method in each activity. But it is not working properly. Can anyone guide me on how to handle it?
You can refer the below link for perfect TabGroupActivity
Handling multiple activies under single tab
This is code for hard back Button of your device. here put your intent.
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
Intent i = new Intent(Activity1.this,Activity2.class);
startActivity(i);
return true;
}
return super.onKeyDown(keyCode, event);
}
Related
I'm new to android and trying to find out how to show a new screen when the user clicks something in the menu item.
I'm using ActionbarSherlock and looking at the sample github-android app.
When the user clicks on an item in the menu, I want to show them a new screen. Github code is doing that like so:
startActivityForResult(new Intent(getActivity(), CreateGistActivity.class), GIST_CREATE);
But I've seen some code samples do:
Intent i = new Intent(getApplicationContext(), SomeActivity.class);
My code looks like this:
public class MainActivity extends SherlockActivity {
....
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.create) {
//show createactivity class
return true;
}
return true;
}
What is the right way to do ?
You can do it just like that:
public class MainActivity extends SherlockActivity {
....
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.create) {
//show createactivity class
Intent i = new Intent(MainActivity.this, SomeActivity.class);
startActivity(i);
return true;
}
return true;
}
startActivityForResult is used when you have to return some value/data to the first screen like a user selection. More here
As far as the context to use getActivity() or getApplicationContext(), I prefer to use the context of current activity MainActivity.this its more straitforward similar to documentation example
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
Inside a fragment use getSherlockActivity() instead of getActivity() as getActivity() can cause crashes to older devices.
Of course getApplicationContext() would always work and not crash but I feel that it may mess the garbage collector and do not let activities to be cleared (but not sure about it)
Just use startActivityForResult
There is no 'right' way. The Github code doesn't first declare the variable. The onther does. I believe for a menu, you normally need to declare the Intent as a local variable, if not a field.
Create an intent: Intent i = new Intent(MainActivity.this, CreateGistActivity.class);
where MainActivity is the activity you're in, and CreateGistActivity is the class you want to launch.
Then use startActivity(Intent) to launch the new activity: startActivity(i);
Or just combine them:
startActivity(new Intent(MainActivity.this, CreateGistActivity.class));
Full code:
public class MainActivity extends SherlockActivity {
....
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.create)
{
Intent i = new Intent(MainActivity.this, CreateGistActivity.class);
startActivity(i);
return true;
}
return true;
}
startActivityForResult probably isn't needed in your case, unless you're expecting to send values between the classes.
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.
Let's say: I have 2 classes MainActivity and SecondActivity
At first, my app starts with an instance of MainActivity. In MainActivity, there're several buttons which will start a SecondActivity if they're clicked. And everytime start a SecondActivty, I put a diffirent parameter by putExtra function to that SecondActivity, so every SecondActivity will display every different content.
And I used this code in SecondActivity:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
moveTaskToBack(true);
return true;
}
return super.onKeyDown(keyCode, event);
}
So everytime back button is pressed in a SecondActivity, it won't be destroyed.
Now, for example, I have this scenario:
MainActivity ==click_a_button==> SencondActivity(instance1)
==press_back_button==> MainActivity ==click_another_button==> SecondActivity(instance2) ==press_back_button==> MainActivity
(*)
Stop at step (*): from here (in MainActivity), user presses the button which started instance1, my question is how to bring exactly that SecondActivity(instance1) to front?
P/S: I read about Intent.FLAG_ACTIVITY_REORDER_TO_FRONT but don't know how to use it in my case.
Set the lanchMode of Your SecondActivity to SingleTop.
<activity
android:name=".SecondActivity"
android:launchMode="singleTop" >
</activity>
I am overwriting this function:
#Override
public boolean onKeyUp(int keyCode, KeyEvent event)
But is there a way to tell which Intent or screen the back button is about to take the user to?
You'd probably better off adding information into the intent that launched your current activity.
Activity A
Intent lIntent = new Intent();
lIntent.putExtra( EXTRAS_ACTIVITY_KEY, ACTIVITY_IDENTIFIER );
startActivitiy...
Activity B
public boolean onKeyUp(int keyCode, KeyEvent event){
Intent lIntent = getIntent();
if(lIntent.hasExtra(EXTRAS_ACTIVITY_KEY) &&
ACTIVITY_IDENTIFIER.equals(lIntent.getStringExtra(EXTRAS_ACTIVITY_KEY)){
// Great we know that it was a Activity A...
... your code here....
}
You could retrieve it from the Activity Stack using the ActivityManager.
List<CharSequence> theActivityTrace = getActivityTrace(null);
this the function that return a list of activities trace which are on stack and you can call the below method as above amd mAct is your current context.
public LinkedList<CharSequence> getActivityTrace(LinkedList<CharSequence> aTrace) {
if (aTrace==null)
aTrace = new LinkedList<CharSequence>();
aTrace.add(mAct.getLocalClassName()+" ("+mAct.getTitle()+")");
if (mAct.getCallingActivity()!=null)
aTrace.add(mAct.getCallingActivity().toString()+" ("+mAct.getIntent().toString()+")");
else if (mAct.getCallingPackage()!=null)
aTrace.add(mAct.getCallingPackage().toString()+" ("+mAct.getIntent().toString()+")");
return aTrace;
}
I have a Class A which runs activity via startActivityForResult by passing Intent to it. In other class, lets say B I get this Intent and recreate activity by it. How can I listen events for that activity, e.g. activity which was started for result is running and user pressed "back" button so I want to do some action.
How can I do this?
Thank you on advance.
Activity in which i recreate instance of object is not derived from Activity class. It is just ACTIVITY. So I have only object. is there any way to do such stuff with instance of class but not a class?
You should override the method : onBackPressed() of the Activity class.
In the activity, where you want to act on "back" button, simply override onKeyDown (or onKeyUp) method:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
//do whatever you need for the hardware 'back' button
return true;
}
return super.onKeyDown(keyCode, event);
}
Keep in mind that if you want the "back" key to still end your activity, then you'll need to include
setResult(result); //if you want to pass a result to activity A
finish();
somewhere in that conditional before return true;
You can override onDestroy and put the code there.
Another possibility (and may fit your needs better) is to override onBackPressed.