Android tabactivity problem - android

I'm having some difficulties getting the TabActivity to work. Here's the implementation of the class:
public class Profile extends TabActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final TabHost tabHost = getTabHost();
if (tabHost.isEnabled()) {
Log.e("profile", "enabled");
}
tabHost.addTab(tabHost.newTabSpec(getString(R.string.friendReqs))
.setIndicator("requests")
.setContent(new Intent(this, FriendRequests.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));
tabHost.addTab(tabHost.newTabSpec(getString(R.string.friends))
.setIndicator("photo list")
.setContent(new Intent(this, Settings.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));
Log.e("profile", "add tabs");
tabHost.invalidate();
}
}
The problem is that I call this class from my main activity (which is a MapActivity if it matters) and when I do the TabActivity doesn't show. It registers the click on the option menu and it even starts the intent but the screen doesn't change..it just stays on the main activity and i see in the logs that the main activity gets resumed. I call it like this:
Intent p = new Intent(this,Profile.class);
p.putExtra(DBAdapter.KEY_USERID,userid);
startActivity(p);
Like I said...there are no errors (the classes called from the tabs exist of course), just no actions. I put some log commands into the onCreate function in the tabactivity (as you see) and they all get written into the log...I have no idea what I'm doing wrong here. Any help?

After some tries...
I set the first tab to a view instead of an intent and it showed the tabs...i could also select the 2nd tab.
Got it. The problem was the Activity being called in the 1st tab. It closed if there was no data passed to it (bundle). Thanks again for the help.

Remove
tabHost.invalidate();
Also you can drop
Intent.FLAG_ACTIVITY_CLEAR_TOP
I don't see any purpose of this, as you need to see the activity anyway when you click on tabs.

I don't see setContentView() in your Activity, which you need.

Related

Change text content depending on button clicked within the same class

My problem is exactly like this link but it's not in android.
I have one button on a layout and two buttons on an another one. On my application, ClickScreen activity can be triggered by either FirstCase activity or SecondCase activity.
I tried to make a conditional statement on my ClickScreen for which activity is triggered but couldn't handle it. I don't want to create two more classes to do this since it's not an efficient technique.
private void goTo2ndPage() {
Intent i3 = new Intent(this, ClickScreen.class);
startActivity(i3);
}
public class ClickScreen extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.click_screen);
}
}
As we discussed in comments. It looks like what you really want is to add extra data in your intent so that Started Activity can get it and act accordingly.
Check out this post !

Navigate Between Activities (Android Studio)

I got a problem when I navigate between two activities, it shows me error and I don't know what is the problem. I am very sure that my code is correct, because it just simple Intent navigate by on click Button.
When I Press the button to go to the next activity it returns me to the fist activity (not the desire one). Note that both activity has background image.
Fist Activity
public class firstActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first_activity);//has a background img and one button
}
public void nextPage(View view){
Intent StartNewActivity = new Intent(firstActivity.this, secondActivity.class);
startActivity(StartNewActivity);
overridePendingTransition(R.layout.slide_in_up, R.layout.slide_out_up);
}
}
Second Activity
public class secondActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_activity);// has a background img and one button
}
public void nextPage(View view){
Intent StartNewActivity = new Intent(secondActivity.this, thirdActivity.class);
startActivity(StartNewActivity);
overridePendingTransition(R.layout.slide_in_up, R.layout.slide_out_up);
}
}
This is the error message
Could not find class 'android.graphics.drawable.RippleDrawable', referenced from method android.support.v7.widget.AppCompatImageHelper.hasOverlappingRendering
Also, I did not use any ripple drawable in my app.Even though I don't know what does it mean?
Thanks,
Something you have wants to to find a refwrence to that ripple component, you need to find out what.
Otherwise, you can try to make sure you have added a reference to the support.v7.widget in the second activity and see if the exception goes away.
Aside from that, we would need to see more code to help further.
When I Press the button to go to the next activity it returns me to the fist activity (not the desire one)
It means that your app crashes when loading your new activity, so it comes back the first one.
Check your activity layout, style configuration => clean your project => Run again.
Hope it can help.
I solved my problem by resizing the background images of the activities, and I added this extra attribute in the manifests file
<application
android:largeHeap="true" >
</application>

how to go back to previous activity without re-rendering it

I have a main activity that forks a second activity, I wanted to go back from second activity to the main activity without reinitialize it. In other words, I wanted to immediately show the main activity after second activity disappears, instead of re-rendering the first activity. I have tried methods like finish, onBackPressed, none of this goes back directly achieved the effect I desired. Rather it seems that they go back and re-render the activity , which feels like all your previous data that renders the view is lost, and it has to do it from scratch again.
So How can I go back directly without re-rendering again?
Thanks
----------------------Update--------------------
Clase MainActivity extends Activity{
public onCreate(Bundle savedInstanceState){
super.onCreate(SaveInstanceState);
setContentView(R.layout.mainview);
GoogleMap gmap = ...
Route route = ...
//draw a route in the google map
// now the map bears a route on it
Intent intent = new Intent(this, secondActivity.class)
}
}
Class SecondActivity extends Activity{
public onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.secondview);
Button backbut =(Button) findViewById(R.id.backbutton);
}
//backbut is associated with a call back onClick in the xml
private onClick(View){
finish();//after this the second activity disappears, but previously drawn route on google map also disappears, and it takes time to redraw them again. feels like onCreate in firstActivity is reentered again
}
}
You need to use this two flag to accomplish what you want:--
Intent.FLAG_ACTIVITY_CLEAR_TOP
Intent.FLAG_ACTIVITY_SINGLE_TOP
Just use this code:--
Intent intent = new Intent(SecondActivity.this,
MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
you should not call finish() on going second activity then the first activity is running on background and after calling finish() on second activity the first activity appears without re-initialize as privious state.
Hop this will help you.
MainActivity extends Activity{
Context context;
oncreate(){
context = this;
Intent intent = new Intent (context , SecondActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
/* here finish() method is not called, if called MaintActivity objects will destory*/
}
}
Guys thank you all for your kind help! I think I kinda solved my problem. They key is that I made the onClick method private so my xml cannot find my onClick and issues an Exception, which caused activity to destroy and re-render themselves. Hope this explanation makes sense

Pressing back button stops and destroys my Activity insted of resuming it

I' working on an app with TabGroupActivity.
I'm launching through a tabhost activies so i can have more than one Intents in each tab:
public class MainTabActivity extends TabActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_tab);
final TabHost tabHost = getTabHost();
tabHost.addTab(tabHost.newTabSpec("tab2")
.setIndicator("Que")
.setContent(new Intent(this, TabGroup2Activity.class)));
}
TabGroup2Activity class:
public class TabGroup2Activity extends TabGroupActivity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent i = new Intent(getApplicationContext(),QueActivity.class);
startChildActivity("categorias", i);
}
}
I got the TabGroupActivity from this page:
My issue is when i'm clicking on the second tab, i get my QueActivity.class opened. It's basically a listview with items retrieved from a Data Base. When i clic a row i get a new intent opened with information passed by Bundle object.
The problem is when i hit the back button in this intent, i go back to the QueActivity Intent, which was on onPause() event, but it goes to onStop(), onDestroy() and onStart() event insted going to other state (onResume i think) where there is no need to be created again. The issue is the Intent being created again executes SQL querys and things that i don't need to execute anymore.
I'd like to press the go back and retreive the last intent in a way i don't have to create it again.
I hope I've explained myself succesfully.
Make sure that you have closed the CURSOR off DB in your activity wherever you have used it.
stopManagingCursor(c);
Try this:
<activity ... android:launchMode="singleTop" />
In your manifest file.
This thing is device specific, some devices didnt go to onresume, and start life cycle from start, To handle that you can use Savedinstatnce, to save a state .

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

Categories

Resources