tabbed activity overrides by activity when follow android tabbed activity.
I am a newbie in android. I am learning to include some activities on different tabs. however, the activity gets fullscreen and override the main tab
// TabMain.java
public class TabMain extends AppCompatActivity {
.......
}
// SectionsPagerAdapter.java extends FragmentPagerAdapter
#Override
public Fragment getItem(int position) {
switch (position){
case 1:
FragmentTab1 tab1 = new FragmentTab1();
return tab1;
default:
return PlaceholderFragment.newInstance(position + 1);
}
}
public class FragmentTab1 extends Fragment {
#Override
public View onCreateView(
#NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.galery_main_layout, container, false);
Intent intent = new Intent(getActivity(), UploadMain.class);
getActivity().startActivity(intent);
return root;
}
}
the UploadMain.java implement Activity
<application
android:icon="#drawable/icon"
android:label="#string/app_name">
<activity
android:name=".TabMain"
android:label="#string/title_activity_tab_main"
android:theme="#style/AppTheme.NoActionBar">
</activity>
<activity
android:name=".FdActivity"
android:configChanges="keyboardHidden|orientation"
android:label="#string/app_name"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".UploadMain"
android:configChanges="keyboardHidden|orientation"
>
</activity>
</application>
I am expecting for the upload main class to be inside tab2
TabMain
| Tab 1 (other activity)
| Tab 2 (UploadMain)
You can't have a Activity inside a ViewPager, it must be a fragment.
And also, when you start activity, like this,startActivity(intent) it will not replace the previous screen or activity, it will just create a new activity with a new layout, having the previous activity paused (and also sometimes stopped) until you close the previous activity. You need to learn more about activity and fragment lifecycles
Your FragmentTab1 starts a new Activity instead of only replacing the View inside your framelayout you start a whole new Activity. Your Code belongs inside the class Fragmenttab1 and the line
Intent intent = new Intent(getActivity(), UploadMain.class);
getActivity().startActivity(intent);
should be removed
Related
I have just gotten into Android app development and have been working on parts of my project in bits and pieces.
I first created my onboarding screens and then made my animated splash screen. However, when I run my application my splash screen does not show and instead loads the onboarding pages first. How can I fix this to make my app first transition through the splash screen before moving on the onboarding screens?
This is my SplashActivity Class code
public class SplashActivity extends AppCompatActivity {
private static int SPLASH_SCREEN = 5000;
// Variables
Animation topAnim;
ImageView image;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_splash);
// Animations
topAnim = AnimationUtils.loadAnimation(this,R.anim.top_animation);
// Hooks
image = findViewById(R.id.splashScreenLogo);
image.setAnimation(topAnim);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent intent = new Intent(SplashActivity.this, OnboardingActivity.class);
startActivity(intent);
finish();
}
}, SPLASH_SCREEN);
}
}
This is my OnboardingActivity Class code
public class OnboardingActivity extends AppCompatActivity {
private OnboardingAdapter onboardingAdapter;
private LinearLayout layoutOnboardingIndicators;
private MaterialButton buttonOnboardingAction;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_onboarding);
// Onboarding Screens
layoutOnboardingIndicators = findViewById(R.id.layoutOnboardingIndicators);
buttonOnboardingAction = findViewById(R.id.buttonOnboardingAction);
setupOnboardingItems();
final ViewPager2 onboardingViewPager = findViewById(R.id.onboardingViewPager);
onboardingViewPager.setAdapter(onboardingAdapter);
setupOnboardingIndicators();
setCurrentOnboardingIndicator(0);
onboardingViewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
#Override
public void onPageSelected(int position) {
super.onPageSelected(position);
setCurrentOnboardingIndicator(position);
}
});
buttonOnboardingAction.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(onboardingViewPager.getCurrentItem() + 1 < onboardingAdapter.getItemCount()) {
onboardingViewPager.setCurrentItem(onboardingViewPager.getCurrentItem() + 1);
}
else {
startActivity(new Intent(getApplicationContext(), SignUpActivity.class));
finish();
}
}
});
}
}
Edit - My Manifest File
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mentalhealthapp">
<application
android:allowBackup="true"
android:icon="#drawable/treen_app_logo"
android:label="treen"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".SplashActivity"></activity>
<activity android:name=".HomeActivity" />
<activity android:name=".SignInActivity" />
<activity android:name=".SignUpActivity" />
<activity android:name=".OnboardingActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
What should I change in my Manifest File?
You need to set the Launch activity as SplashActivity in your AndroidManifest.xml file
<activity
android:name=".SplashActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
I have a problem while hitting the back button on an activity, in particular, the back button on the activity closes the application and destroys all the activities, I want to go back in the stack, and in the previously called activity when hitting the back button.
Here is my code:
ChatToolBar = (Toolbar) findViewById(R.id.chat_bar_layout);
setSupportActionBar(ChatToolBar);
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowCustomEnabled(true);
LayoutInflater layoutInflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View action_bar_view = layoutInflater.inflate(R.layout.chat_custom_bar, null);
actionBar.setCustomView(action_bar_view);
My AndroidManifest.xml:
<activity
android:name=".ChatActivity"
android:parentActivityName=".ProfileActivity" />
I also tried:
<activity
android:name=".ChatActivity"
android:parentActivityName=".ProfileActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".ProfileActivity" />
</activity>
Where should be the problem?
I just solved it with this code:
#Override
public boolean onOptionsItemSelected(#NonNull MenuItem item)
{
int id = item.getItemId();
if (id == android.R.id.home)
{
SendUserToMainActivity();
}
return super.onOptionsItemSelected(item);
}
private void SendUserToMainActivity()
{
Intent mainIntent = new Intent(ChatActivity.this, MainActivity.class);
mainIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK );
startActivity(mainIntent);
finish();
}
you can add button back in action bar
in mainfest.xml
<activity .ActivityB" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".ActivityA" />
I am creating an app where a user can generate a random number from 1 to 90 to play bingo or tambola. Here I set the homeAsUpEnabled as true, and in the Manifest, I have added the support parent activity:
this is the main activity:
ActionBar actionBar = getSupportActionBar();
assert actionBar != null;
actionBar.setTitle("Full House: Housie Number Generator");
actionBar.setDisplayUseLogoEnabled(true);
actionBar.setDisplayShowHomeEnabled(true);
ivBoard.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v) {
Intent intent = new Intent(NumberActivity.this, com.app.fullhouse.Board.class);
intent.putExtra("doneNumbers", doneNumbers);
startActivity(intent);
}
});
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.reset:
new AlertDialog.Builder(this)
.setTitle("Reset")
.setMessage("Press OK to reset Board")
.setNegativeButton(android.R.string.no, null)
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface arg0, int arg1) {
recreate();
}
}).create().show();
break;
case R.id.showNoOrder:
Intent intent = new Intent(this, com.app.fullhouse.NumberOrder.class);
intent.putExtra("doneNumbers2", doneNumbers2);
startActivity(intent);
break;
}
return super.onOptionsItemSelected(item);
}
}
and this is my other activity:
ActionBar actionBar = getSupportActionBar();
tvnumbers = findViewById(R.id.tvNumbers123);
assert actionBar != null;
actionBar.setTitle("Sequence of Number generated");
actionBar.setDisplayUseLogoEnabled(true);
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
and part of another:
ActionBar actionBar = getSupportActionBar();
actionBar.setTitle("Board");
actionBar.setDisplayUseLogoEnabled(true);
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
please take into notice that I have cut short much of the unrequired items because the code would get too long
What have I tried:
I tried to startActivityForResult and their end activity by guessing that the back button could be
android.R.id.home
when I try to press the back button on the phone, noting goes wrong and the previous data is still there
but when I press the back button in any of the two-child activities, it goes back, but it recreates the whole activity, hence the game is lost
here is the manifest in case you need it :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.app.fullhouse">
<supports-screens
android:anyDensity="true"
android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true" />
<application
android:allowBackup="true"
android:icon="#mipmap/fullhouse"
android:label="#string/app_name"
android:roundIcon="#mipmap/fullhouse"
android:supportsRtl="true"
android:theme="#style/Theme.AppCompat.DayNight.DarkActionBar">
<activity android:name=".GenerateTicket"></activity>
<activity android:name=".NumberOrder">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".NumberActivity" />
</activity>
<activity android:name=".NumberActivity"></activity>
<activity android:name=".Board">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".NumberActivity" />
</activity>
<activity
android:name=".MainActivity"
android:exported="true"
android:theme="#style/Theme.AppCompat.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
as well as the menu
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/reset"
android:menuCategory="container"
android:orderInCategory="104"
android:title="#string/reset_board"
app:showAsAction="never" />
<item
android:id="#+id/showNoOrder"
android:title="Show sequence of numbers"
app:showAsAction="never"
android:orderInCategory="105"/>
</menu>
thank you in advance
Possibly you can find your answer here
How to come back to First activity without its onCreate() called
And can try below from here
Two cases:
If you want to keep other activities live and just want to bring A to front, just use intent flag FLAG_ACTIVITY_REORDER_TO_FRONT.
If you don't want to clear all activities and want an existing instance of A at the top, then use intent flags
FLAG_ACTIVITY_CLEAR_TOP and FLAG_ACTIVITY_SINGLE_TOP.
Note: If you use only FLAG_ACTIVITY_CLEAR_TOP, then onCreate will be
called.
I have found an answer (to all the people who visit this later on :))
the answer is this
Intent intent = new Intent(this, Activity2.class);
intent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT|Intent.FLAG_ACTIVITY_REORDER_TO_FRONT|Intent.FLAG_FROM_BACKGROUND);
startActivity(intent);
and in the manifest file, you don't need to add anything
cause here it's working for me
I've created 3 buttons. Each should open up different activities. I've tested it on my device and it never opens a new activity. Could it be due to the main.xml file where I've used the onClick feature for the button despite no using it within my main activity.
public class Main extends Activity implements View.OnClickListener{
private Button playButton, rulesButton, aboutButton;
#Override
protected void onCreate(Bundle savedInstanceState) {//when the app starts this method is run
super.onCreate(savedInstanceState);
// Set the layout for fragment_layout.xml
setContentView(R.layout.main_layout);
playButton = (Button) findViewById(R.id.button_play);
playButton.setOnClickListener(this);
rulesButton = (Button) findViewById(R.id.button_rules);
rulesButton.setOnClickListener(this);
aboutButton = (Button) findViewById(R.id.button_about);
aboutButton.setOnClickListener(this);
}
public void buttonPlayClick(){
startActivity(new Intent("com.example.will.sata.openGLActivity"));
}
public void buttonRulesClick(){
startActivity(new Intent("com.example.will.sata.DetailsActivity"));
}
public void buttonAboutClick(){
startActivity(new Intent(""));
}
#Override
public void onClick(View v) {
switch (v.getId())
{
case R.id.button_play:
buttonPlayClick();
break;
case R.id.button_about:
buttonAboutClick();
break;
case R.id.button_rules:
buttonRulesClick();
break;
}
}
}
AndroidManifest.xml
<activity
android:name=".Main"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.will.sata.DetailsActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity
android:name="com.example.will.sata.openGLActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
main.layout.xml
android:id="#+id/button_play"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Play"
android:onClick="PlayGame"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip"
style="?android:attr/borderlessButtonStyle"
android:focusable="true" />
I have some corrections of your code, I hope it helps:
In main_layout.xml you don't need android:onClick="playGame" because you are using View.OnClickListener in the main class.
You can also use the android:onClick="playGame", but your code in the Main.class would be like this:
public class Main extends Activity {
private Button playButton, rulesButton, aboutButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
playButton = (Button) findViewById(R.id.button_play);
rulesButton = (Button) findViewById(R.id.button_rules);
aboutButton = (Button) findViewById(R.id.button_about);
}
public void playGame(View v) {
switch (v.getId())
{
case R.id.button_play:
Intent intent = new Intent(Main.this, OpenGLActivity.class);
startActivity(intent);
break;
case R.id.button_rules:
Intent intent = new Intent(Main.this, DetailsActivity.class);
startActivity(intent);
break;
}
}
To register a new activity in AndroidManifest.xml you just need to do this
<activity android:name="com.example.will.sata.OpenGLActivity"/>
<activity android:name="com.example.will.sata.DetailsActivity"/>`
To start a new activity from Main.class you have to do this (It is really important to register the Activity first in the AndroidManifest.xml):
Intent intent = new Intent(Main.this, OpenGLActivity.class);
startActivity(intent);
Intent intent = new Intent(Main.this, DetailsActivity.class);
startActivity(intent);
Tip: be careful with the naming conventions
I'm trying to do something rather simple here, just launch a new activity from my main one. Here's the code:
public class mainActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent i = new Intent(mainActivity.this, testActivity.class);
startService(i);
}
}
///////////////// next file /////////////////
public class testActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
Toast.makeText(this, "testActivity started", Toast.LENGTH_SHORT).show();
}
}
///////////////// manifest section ///////////////////
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".mainActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".testActivity" />
But I never see the Toast from testActivity - what gives?
You want to use startActivity instead of startService
Intent i = new Intent(mainActivity.this, testActivity.class);
startActivity(i);
To start an activity you should use startActivity() instead of startService().
You'll also have to make sure the testActivity is listed in your android manifest.
If the activity is still running in the background, it gets called and only the onResume() gets called, not the onCreate();
check the lifecycle here: http://developer.android.com/guide/topics/fundamentals/activities.html#Lifecycle