Too many Activities in Android App. Any alternatives? - android

I am creating my first app. In this app, I have an expandable list with many items. When I select any of these items, I want several paragraphs to be displayed. Do I need to create an Activity for each of these items if text is the only thing I want displayed? I know that there has to be an easier way. I did create it like this at first and it seemed very bulky (30+ activities), so now I have it set up so that when an item is selected, the setContentView opens the corresponding layout with the text that needs to be displayed. This works but there is a catch, whenever I hit the back button, it takes me back to my main activity class and not my expandable list class. I want the user to be able to go back and select something else from the list. Any guidance as to what I need to do would be appreciated.

I would suggest creating string resources for each item you would like to display, then creating one activity with a TextView. Then, instead of creating new intents for each activity, create an intent that goes to the new activity, and add an extra that contains the text for the TextView. For example:
Activity1:
myButton.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
Intent intent = new Intent(this, ParagraphView.class);
intent.putExtra("textData", getResources().getString(R.string.myText));
getBaseContext().startActivity(intent);
}
});
In the onCreate of the viewer, add this to get your TextView:
Intent intent = getIntent();
String textData = intent.getStringExtra("text");
Now, we need to write the text into the TextView:
TextView tv = (TextView) findViewById(R.id.myTextView);
tv.setText(textData);
All you have to to is set up your string resources and button click listeners. You may consider this easier than having lots of activities (it's definitely easier to manage entries this way) but does require a bit of setup.
Edit: Thanks to #ianhanniballake for pointing out a much better way (I don't even know what I was thinking at the time...)
Edit2: Wow, I REALLY messed up my code. (Hopefully) Fixed now

Related

What's the correct way of displaying ViewPager after associated ListView's item click?

I'm a beginner in Android, so I apologize for the mistakes and I'd appreciate any constructive criticism.
I'm writing a basic application with a ListView of images, and when the user clicks on an item in the list, I want to display that image in a ViewPager, where the user can swipe back and forth to browse the whole list of images. Afterwards when the user presses the back button, I want to switch back to the ListView.
I manage the business logic in the MainActivity, which uses MainActivityFragment for the ListView and ImageHolderFragment for ViewPager.
The simplified code so far is as follows:
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListItems = new ArrayList<>();
mListItemAdapter = new ListItemAdapter(this, R.layout.list_item, R.id.list_item_name, mListItems);
mListView = (ListView) findViewById(R.id.list_view_content);
mListView.setAdapter(mListItemAdapter);
mDeletedListItems = new ArrayList<>();
mViewPager = (ViewPager) getLayoutInflater().inflate(R.layout.image_display, null, true);
mImageAdapter = new ImageAdapter(getSupportFragmentManager(), mListItems);
mViewPager.setAdapter(mImageAdapter);
mViewPager.setOffscreenPageLimit(3);
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
mViewPager.setCurrentItem(position);
setContentView(mViewPager); // TODO: this is very wrong!
}
});
loadImages();
noContentText = (TextView) findViewById(R.id.no_content_text);
if (mListItems.isEmpty()) {
noContentText.setText(R.string.no_images);
} else {
mImageAdapter.notifyDataSetChanged();
}
}
Although this does work to some extent, meaning that it manages to display the ViewPager when an item in the list is clicked, there are two things about it ringing the alarm bells:
I've read that calling setContentView() for the second time in the same class is pretty much a sin. Nobody explained me why.
The back button doesn't work in this case. When it's pressed, the application is terminated instead of going back to the list view. I believe this is connected to the first point.
I would appreciate any help, explanations if my idea is completely wrong, and if my case is hopeless, I'd like to see a successful combination of ListView and ViewPager with transitions between each other.
Your activity already has R.layout.activity_main set as content view, which rightly displays the list view - that's what the responsibility of this activity is as you defined it. If we want to change what's shown on the screen, we should use a different instance of a building block (activity or fragment) to display the view pager images.
To say the least, imagine if you wanted to change the view to a third piece of functionality or UI, or a fourth... it would be a nightmare to maintain, extend and test as you're not separating functionality into manageable units. Fields that are needed in one view are mixed with those needed in another, your class file would grow larger and larger as each view brings its click listeners, callbacks, etc., you'd also have to override the back button so it does what you want - it's just not how the Android framework was designed to help you. And what if you wanted to re-use UI components in different contexts whilst tapping in to the framework's activity lifecycle callbacks? That's why fragments were introduced.
In your case, the list view could continue to run in your MainActivity and in your click listener, onItemClick you could start a new activity that will hold a viewPager:
Intent i = new Intent(MainActivity.this, MyLargePhotoActivityPager.class);
i.putExtra(KEY_POSITION, position);
// pass the data too
startActivityForResult(i, REQUEST_CODE);
Notice how you could pass the position to this activity as an int extra, in order for that second activity to nicely set the viewPager to the position that the user clicked on. I'll let you discover how to build the second activity and put the ViewPager there. You also get back button functionality assuming your launch modes are set accordingly, if needed. One thing to note is that when you do come back to the list View, you'd probably want to scroll to the position from the view pager, which is why you could supply that back as a result via a request code. The returned position can be supplied back to the list view.
Alternatively, you could use the same activity but have two fragments (see the link further above) and have an equivalent outcome. In fact, one of your fragments could store the list view, and the second fragment could be a fullscreen DialogFragment that stores a viewPager, like a photo gallery (some details here).
Hope this helps.
I've read that calling setContentView() for the second time in the
same class is pretty much a sin. Nobody explained me why.
Well, you kind of get an idea as to why.
When you use setContentView() to display another 'screen' you do no have a proper back stack.
You also keep references to Views (like mListView) that are not visible anymore and are therefore kind of 'useless' after you setContentView() for the second time.
Also keep in mind orientation changes or your app going to the background - you'll have to keep track of the state that your Activity was in which is way more complicated than it has to be if you have one Activity that does two different things.
You won't be arrested for doing things like you do right now, but it's just harder to debug and keep bug free.
I'd suggest using two different Activities for the two different things that you want to do, or use one Activity and two Fragments, swapping them back and forth.
If you insist on having it all in one Activity you need to override onBackPressed() (called when the user presses the back button) and restore the first state of your Activity (setContentView() again, pretty much starting all over).

Some GridView cells fade and disappear after ShareActionProvider intent

I have a GridView where each cell is a thumbnail, a filename and a checkbox. To populate the grid I use a CustomCursorAdapter:
The CustomCursorAdapter extends CursorAdapter.
I have a floating button that launchs the system camera. If I take a picture, the grid updates correctly with the new cell.
In the action bar I have a delete button. If I check one or more checkboxes and press the delete button the cell is/are correctly deleted.
But then I have also a ShareActionProvider button so you can check multiple cells and share them. And that works. BUT, here comes the problem. Imagine we have 3 or 4 cells. You check a couple of them, share them, and when you come back from the sharing dialog you see all the cells and you can see how all of them fade and disappear but one, the upper leftmost one.
I checked and saw that after the sharing dialog is the onResume method the one executed. The only thing I'm interested to be done in that method is to reset the checkboxes (a selected.clear() of the variable and a cb.setChecked(false) of each Checkbox view). Also I set the intent of the ShareActionProvider button for a empty selection (this will trigger a Toast: "Select first the images to share" if the button is pressed now).
#Override
public void onResume() {
if (shared) { //we come from a share
uncheck(); //unchecks the Checkboxes views
selected.clear(); //reset the selected items list
//getShareIntent returns an intent with the items in
//selected (now empty) as extras
Intent intent = this.getShareIntent();
if (intent != null && mShareActionProvider != null) {
mShareActionProvider.setShareIntent(Intent.createChooser(intent,
getResources().getText(R.string.send_to)));
}
shared = false;
}
super.onResume();
}
Removing the unchecking, reset and new intent setting don't affect to that behaviour, the cells disappear anyway. In fact, if I comment the whole onResume function the problem remains.
I tried with things like:
grid.invalidateViews();
mAdapter.notifyDataSetChanged();
grid.setAdapter(mAdapter);
Also tried and put the super as the first line, but then checkboxes, intent and list variable aren't updated.
Another clue is that if I press the thumbnail of the unique cell left visible (this loads a new activity to see the picture big size), then the disappeared cells appear for a second while the new activity is loading. o_O
I tried to press the places where the thumbnails should appear after the share to check if it launches the big display activity anyway, but that doesn't happen.
I read about similar things when scrolling GridViews but I just have three elements, no need to scroll. What could be happening here?
EDIT: To check if the issue was related to the memory and the thumbnails, I commented them in the item views, letting just the textview and the checkbox. But the problem is still there. Absolutely desperate -I've been days trying to fix this-I tried to relaunch the grid activity in the onResume, to try to force the redraw (while mAdapter.notifyDataSetChanged() worked when adding a new cell, never worked after the sharing):
#Override
public void onResume() {
super.onResume();
if (shared) {
shared = false;
Intent intClearStack = new Intent(
getBaseContext(),
GridActivity.class);
intClearStack
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intClearStack);
finish();
}t
}
I'm not proud of this solution but is the nearest thing I found to fix this. Nearest because, while it works almost all the time, ocasionally it doesn't. I'd like to know why there's a difference when it comes back from a ShareActionProvider activity and any other activity, to try to find out why this is acting like that.

Android: Dynamically Change Activity Title to Text of Selected List Item

I'm new to Android development.
I created a simple master-detail app that starts with a simple, vertical scrolling list of topics.
When the user selects a topic, a details screen appears, replacing the first screen, with a list of details that pertain to the selected topic.
I want the title for the details screen to show the topic the user has selected on the first page, but haven't been able to solve the problem after working for almost a week.
All I need to know is, Can this be done? Not looking for someone to solve this for me, but maybe a hint or a link to a tutorial that shows how this can be done.
Note: I'd post a drawing of what I want to do, but I'm new here and don't have 10 reputation yet.
Thanks,
SonCoder
Not exactly sure what you want but either way..
-You have a listview. Each view (the data) in the listview should be a represented by a model. (aka a separate class containing specific information that you want to represent for each listitem.
-Write a custom list adapter (extend from base adapter).
http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/
In the getView method of this class you load the the String field of the model that you want in the textview.
-Make sure to use the viewholder pattern in the adapter above. I noticed the example doesnt use one. This speeds up scrolling in the list because there are much fewer calls to findViewById.
-in the list activity set up a View onClick listener. This should create an intent (for launching an activity) or a fragment transaction (for fragments). Send the instance of your entire model (will get from
parent.getAdapter().getItem(position);
in the on click method) into the detail activity.
-if you want to set a textview title just get the textview and set it from the model. It will be the same filed you inflated in the getView method of the adapter.
-if you want to set the titile in the actionbar set:
this.getActionBar().setTitle(title)
This is simple. Just send extra data in the intent that starts the activity and then in the activity's onCreate read the data and then use the setTitle(myString) method from the activity.
setTitle(String title) can be called from anywhere using the activity by the way.
So, your in your listadapter, then you set a listener on your view right? A simple onClickListener on the whole "root" view is just fine.
In the listener you say something in the ways of this:
Intent intent = new Intent(myActivity, MySubActivity.class);
intent.putExtra(key, titleName);
myActivity.startActivity(intent);
Note that the activity reference should be set in the constructor of the adapter and that the "key" String is something you get from your strings.xml. Do not duplicate these in code since if you change one and forget to change the others you might get some wierd NPEs.
Continue in your MySubActivity's onCreate()
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String key = getString(R.string.my_title_key);
String title = intent.getString(key);
setTitle(title);
}
NOTE: I'm not sure of all method names are correct and such but something like this.

Switching between layouts from the same activity: Possible but recomended?

I want to know why do people keep recommending starting new Activities when you want to display another screen?
Let's say I want to display a screen with a label and an edit_text to ask for username, then another similar screen to ask for age, then another screen to display the data entered and ask for confirmation.
I did this:
main_layout.xml: has a button let's say mainButton, onClick="startRegistration"
name_layout.xml: edittext asking for name
age_layout.xml: edittext asking for age
confirm_layout.xml: display info + button to confirm
and in:
public class MainActivity extends Activity {
onCreate(...) {
...
setContentView(R.layout.main_layout);
}
public void startRegistration(View clickedButton) {
setContentView(R.layout.name_layout);
}
..
}
... and so on, all button handlers are public void methods in main class and each method contains setContentView() with the next layout as parameter.
I have a feeling this is bad programming style, however it works perfectly fine. Is it ok to do this? If not, is there any other easy way? Starting a new activity for such things feels really stupid to me.
Normally you group 'activities' together in an Activity. For you, registration uses multiple screens but are linked to each other. I would suggest using 1 Activity with a ViewFlipper.
Having 1 Activity for all will screw up the navigation for the user. The Back key has to be handled specially. "if back key, set this content, else set this content, etc"
If you code different layouts for the same type of screen then its not really an ideal idea. The better idea is to have the same layout and point to the same layout from the classes where the layouts are the same. In the screen where you want to have an extra/less control or different control then just have unique IDs to such controls.
Refer the controls from their IDs and you will have a single layout file. Writing different layout classes where the controls are same will pave way to code repetition and hence is not an ideal way of coding.

Changing a textview in a separate activity in android

I have a log in page that pulls information from a data base, I then want to use this some of this information to populate different textviews on a new page/activity. I can get a textview to change on the activity where I have my submit button, but when I try to change the textview on my second activity, it just crashed (The application has stopped unexpectedly).
Here's my code for changing the textview (where txtID is my textview on a separate activity)
TextView test2 = (TextView) findViewById(R.id.txtID);
test2.setText(test);
my xml for seperate activity
<TextView android:text="TextView" android:id="#+id/txtID"
android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
Oh, I'm using a tableview for my login page, then tabs for my the rest of my pages. I'm pretty new to this, so sorry if this is something simple, but any help would be greatly appreciated!! :-)
You don't want to directly touch the UI elements of another Activity. You can make use of bundles to pass information back and forth. Here is an example:
Say we have Activity A, and it has some information as a String it wants to pass to become the text of a TextView in Activity B.
//Setup our test data
String test = "Some text";
//Setup the bundle that will be passed
Bundle b = new Bundle();
b.putString("Some Key", test);
//Setup the Intent that will start the next Activity
Intent nextActivity = new Intent(this, ActivityB.class);
//Assumes this references this instance of Activity A
nextActivity.putExtras(b);
this.startActivity(nextActivity);
So now in the onCreate method for Activity B, we can get that String and assign it as the text to the TextView like you have
public void onCreate(Bundled savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); //Setup some layout, set to your own
String test = getIntent().getExtras().getString("Some Key");
TextView test2 = (TextView) findViewById(R.id.txtID);
test2.setText(test);
}
I'd probably let each separate Activity take care of its own display, and not try to have Activity 1 directly update the display of Activity 2, which is kind of what it looks like you were doing.
The Notepad Tutorial demonstrates an application with two Activities, where one Activity calls another, passing in data. (Take a look at onListItemClick in Notepadv3.) You could maybe follow this model to pass data from Activity 1 to Activity 2, where Activity 2 then takes care of its proper display, using the data it received.
If you're still having problems (like your application crashing), then please post the complete minimal code necessary to replicate your problem. Note the Notepad Tutorial and the Hello, World Tutorial include steps for debugging, which might help you isolate the exact problem.
Andy... If you try to directly touch a UI widget in another activity, your app will crash. Been there, done that accidentally. Instead, consider passing an immutable stateful object between the activities. This can be done using startActivityForResult for instance. I have some sample code here.

Categories

Resources