Android Empty Activity and Blank Activity - java

I am trying to make an android project and there are many types of activities,
from there I see blank activity and empty activity.
Does anyone know the difference between the two?
Blank activity is not creating main_activity.xml but is created by empty activity.
In empty activity, I am unable to add EditText Field, Does anyone know why I am not able to add it? Error says :
Exception raised during rendering: java.lang.System.arraycopy([CI[CII)V
Exception details are logged in Window > Show View > Error Log
And when I open error log, there is no error

Check which API level you're using for rendering the view. In my case it was API 20 (for Android Wear). I changed it to API 19 and the error is gone.

In my case, same to you blank activity don't generate java&layout but empty activity works well.
EditText could use with API20, API21. (BTW blank activity worked well with previous installed).

Blank Activity: this will create a blank activity with just 1 textview.
Full Screen Activity: This is typically used by games. This activity hides the status bar and then your activity takes all screen space.
Master Detail Flow: This template creates advanced navigation, based on fragments, which will work on both tablets and phones without any change

Related

When I rotate the screen, the fragment is destroyed and returned to Activity, which opened that Fragment. But why he's doing so?

I have an Activity which can open 2 different Fragments by 2 different Buttons. By the default that Activity when it creates, it is opening a Fragment, we call it "The Main Fragment".
The first Fragment to which we are going over by the first Button we meet zero problems with the Rotation, but the second one after the rotation disappears and the screen shows the Main Fragment's content. When I tried to rotate the screen back, I see the Main Fragment's content again. But why it is so, if I didn't write any code, which must return me to the Main Fragment without clicking a button.
What assumptions do you have?
Why this is happening ?
Default Behavior, Actiivty is getting recreated on orientation change so your fragment are.
Explanation
You need to understand Activity Life Cycle to understand why this is happening.
First, “rotating the screen” is not the actual scenario we are talking about today. Because any configuration change will cause Android to restart your Activity. A configuration change might be the device rotating (because now we have a different screen layout to draw upon), or it could be a language switch (because we need to re-write all those strings, which may need more room now OR it could be the scary RTL switch!), or even keyboard availability.
By reloading your app, what the system is actually doing is calling onDestroy() and then immediately calling onCreate(). This way, your Activity is as fresh as possible, with all of the right creation data (even though the user has been with you the entire time).
Now you have following option -
Either Fix Orientation for your app from AndroidManifest.xml
But oviously that is not a very good experience for user.
Save activityState with onSaveInstanceState()
This method will be called before onDestroy(). And, when your Activity is created, there’s a matching step onRestoreInstanceState(), which will also be called automatically. All of these automatic steps mean that you can let the system worry about saving and loading your data, because you planned ahead and mapped out what was important. (Or, you can skip onRestoreInstanceState() and load your saved state from the Bundle that comes with onCreate().
In you integrate Fragment in activity, because activity is getting destroy() so your fragment will also destroy() and will be recreated.
Please take a good read on Handling Configuration Change and this.
Once you understood the concepts things will start falling into your but it will only happen if you will complete your learning curve.
Happy Coding !
That is because onCreate is being called every time the screen is rotated. Probably you are displaying The Main Fragment from your onCreate method. You will face the same issue if you put your fragment display logic in onResume because just after onCreate, onResume is called.
Solution: store the fragment on top in shared preferences that way you know what to display every time onCreate is being called.

How do I create simple two page Android swipe app in Android Studio

I want to create a tabbed activity or empty activity in Android Studio, but I need to be able to swipe to a page 2.
All I would like is for the app to say the standard "Hello World" on the 1st screen, and then you can swipe to the next page that says "Hello World Page 2", Just those 2 screens with no title bar on any of the screens - that's it.
I've tried creating a tabbed activity and was able to create both pages and swipe works, but it creates a button in the lower right corner and a title bar with settings button up top that I couldn't figure out how to delete.
I tried an empty activity but couldn't figure out how to add another page. If anyone could provide me the code or a sample project I could edit that would be amazing as I've lost several days in frustration over this.
You can use ViewPager component. When you will use it you would to use TabLayout, but if you want to hide it you just simply can edit its visibility.
You Can try this Example.
https://www.sitepoint.com/using-viewpager-to-create-a-sliding-screen-ui-in-android/
Simply replace the image with TextView.
To hide the actionbar user getSupportActionBar().hide()

For supporting different screen size why should i use fragment in android instead of activity

I am beginner to android..I am started new android project..for supporting
different screen size..in fragment documentation they given to use fragment..but
why cant i use activity in android..if i use activity or fragment..which i should i use in this both..please dont give link of activity or fragment..please anyone answer me..i dont know which to use?...i want about all documentation they given about activity and fragment but i dint understand which to use..below is the link i read about fragment..if i use activity i should do more codings?
https://developer.android.com/guide/components/fragments.html
In fact you can't use a Fragment alone, Fragments are inside the Activity.
One point of using the Fragments for supporting different screen sizes is the ability to implement some views like a "Master/Detail" view.
A Fragment, as its name says, is a part of a bigger controller "the Activity", its reference can be removed and it's cleaner than having a big massive Activity to handle all the states of a view.
So the use case is completely depends on your project and its User Interface. I'd be glad to help you if you give me more information about your project and its design.
I think you will need at least one activity. And then for better handling different device rotations and screen sizes you can use one or more fragments inside this activity.
I try to explain this with an example:
You want to create a nice music player app which should look nice in portrait and landscape mode.
You split your app up into three fragments:
Here you can see how the app looks in portrait mode. The activity shows two fragments: The first fragment only consists of a listview. There the song titles are listed. On the bottom you can see the second fragment, which displays the song title of the current playing song and got a button for pausing the music.
When your user uses the music player app on a tablet in landscape mode you have more space for displaying stuff. Then the activity shows the list fragment (which also gets displayed in portrait mode) and it shows a third fragment which shows detailed information about the current playing song (e.g. the album image) and a progress bar.
By using fragments you only need to write the code for the list once.
Sweet and Simple thing, What i recommend is always use Fragments,
But for Fragment you will require Activity.
Take it in this way , Activity is a Canvas on which you can put any number of Fragments.
Whatever your UI is always use Fragment present on a activity if you want to show one screen even then also, So that you will always have Flexibilty to use all those cool things which fragments provides,maybe in future or in current.
If you use activity it has limits,FOR EXAMPLE, LIKE in INSTAGRAM AT BOTTOM, It has FIVE OPTIONS, Suppose THOSE OPTIONS ARE ON A ACTIVITY AND BY CLICKING ON THEM YOU CAN SWITCH TO DIFFERENT FRAGMENTS.
For more info:
Here is the most accepted answer for this topic.

Android - Black Screen after clicking BACK in an Activity with an AltertDialog after Search Activity

I have an Activity which displays an alert dialog priot to the content.
Also a search activity is called before this activity.
Now when i press back on this activity i am lead to a black screen (which i assume is eighter because of the dialog [unlikely] or from the search activity). Only when i press BACK twice i get to the activity i was before.
How can i get rid of this?
Thank you.
This phenomenon was caused by the search activity. to avoid this i simply added android:noHistory="true" for the Search Activity in the Manifest.

Avoid Android automatically re-add my fragment on orientation change

I have a ListFragment which depends on the hosting Activity to properly initialize. On first run, it loads up fine. Once I change the orientation, my app crashes. From the stack trace I can see it isn't me trying to add the Fragment prematurely, rather Android is trying to restore the Fragment.
I have setRetainInstance(false) set in the onStart method but can't find any method to disable the restoring of the Fragment once the orientation changes. Any ideas? Do I need to remove the Fragment prior to my app being destroyed?
Edit: I ended up delaying initializing the list until the Activity is ready. Android conveniently shows a 'loading' message until the adapter is set.
I ended up delaying initializing the list until the Activity is ready. Android conveniently shows a 'loading' message until the adapter is set.
I have a ListFragment which depends on the hosting Activity to properly initialize.
That may be your difficulty right there.
From the stack trace I can see it isn't me trying to add the Fragment prematurely, rather Android is trying to restore the Fragment.
Correct.
Any ideas?
I would focus on handling configuration changes properly. Between onSaveInstanceState() in the fragment and the combination of onRetainNonConfigurationInstance(), getLastNonConfigurationInstance() (both on Activity) and onAttach() (on your Fragment), you should be able to pass whatever stuff is in the old fragment to the new one without crashing. For configuration changes, do not rely upon "the hosting Activity to properly initialize".

Categories

Resources