-1

I have created a Sales Order menu, the menu uses the Activity and Fragment files, The problem is when I display the Customer List page to find customer data that will be used on the SalesOrder page (Create Data tab (use Fragment)) CustomerList page not closed, Here's my code First run show

enter image description here

Then I clik Sales Order show like this, its file SalesOrderActivity.kt Page SalesOrder use TabLayout and Fragment , ie (fragment for Create Data and Fragment List Data) enter image description here

File SalesOrderActivity.kt
    class SalesOrderActivity : AppCompatActivity() {
        lateinit var viewPager     : ViewPager
        lateinit var tabs          : TabLayout
        private var appContext: Context? = null

        override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)
          setContentView(R.layout.activity_sales_order)

          CommonCtx.setViewXml2(contentView)
          setSupportActionBar(findViewById(R.id.my_toolbar))
          supportActionBar!!.title = "Sales Order"
          supportActionBar!!.setDisplayHomeAsUpEnabled(true)

          viewPager    = findViewById(R.id.viewPager)
          tabs         = findViewById(R.id.tabs)
          tabs.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
              override fun onTabSelected(tab: TabLayout.Tab?) {
                  val position = tab?.position
                  if (position == 1) {
                     println("get selected")
                  }
          }

          override fun onTabUnselected(tab: TabLayout.Tab?) {
          }

           override fun onTabReselected(tab: TabLayout.Tab?) {
           }
        })

        appContext = this
        CommonCtx.setActivity(this)
        setUpTabs()
    }


    //SHOW FRAGMENT CREATE DATA AND LIST DATA
    private fun setUpTabs(){
        val adapter = com.myapp.sfa.adapter.ViewPagerAdapter(supportFragmentManager)
        adapter.addFragment(SalesOrderCreateDataFragment(), "Create Data")
        adapter.addFragment(CustomerListDataFragment(),     "List Data")

        viewPager.adapter = adapter
        tabs.setupWithViewPager(viewPager)
        setupTabIcons()
    }

    private fun setupTabIcons() {
        tabs.getTabAt(0)!!.setIcon(tabIcons.get(0))
        tabs.getTabAt(1)!!.setIcon(tabIcons.get(1))
    }

    private val tabIcons = intArrayOf(
        R.drawable.add_sign,
        R.drawable.list_48
    )
    override fun onSupportNavigateUp(): Boolean {
        finish()
        return true
    }

}

in page Sales Order I used fragment for show tab Create Data and List Data this file SalesOderCreateDataFragment.kt

    class SalesOrderCreateDataFragment : Fragment() {

        private var appContext: Context? = null
        private lateinit var txtvCustId  : TextView
        public lateinit var edtCustCode : EditText
        private lateinit var btnCustomer : Button
        lateinit var DBHelper : DBHelper
      
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            appContext = this.getActivity()
            DBHelper = DBHelper(appContext!!))
        }
   
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, 
                              savedInstanceState: Bundle?): View? {
        val view = inflater.inflate(R.layout.fragment_sales_order_create_data,container,false)
        
        txtvCustId  = view.findViewById<TextView>(R.id.txtvCustId)
        edtCustCode = view.findViewById<EditText>(R.id.edtCustCode)
        btnCustomer = view.findViewById(R.id.btnCustomer)
        
             btnCustomer.setOnClickListener {
                 showCustomer()
             }
       
        appContext = this.getActivity()
        return view
    }


    //SHOW LIST CUSTOMER event clik Cust. Code
    fun showCustomer() {
         val intent = Intent(this.getActivity(), CustomerListActivity::class.java)
         //intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP)
         intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
         intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
         intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK)
         CommonCtx.getContext().startActivity(intent)
         CustomerListActivity().finish()
    }

    fun getCustomer(idCustomer: String){
         var customerPojo = CustomerPojo()
         customerPojo= SQLCustomer().getCustomerById(idCustomer)
    }
}

while I click button Cust Code enter image description here

Will show page CustomerListActivity, like this picture Code in Cust code event

enter image description here

File CustomerListActivity.kt
class CustomerListActivity : AppCompatActivity() {
    private lateinit var recyclerView   : RecyclerView
    lateinit var DBHelper : DBHelper

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_customer_list)
        setSupportActionBar(findViewById(R.id.my_toolbar))
        supportActionBar!!.title = "Customer List"
        supportActionBar!!.setDisplayHomeAsUpEnabled(true)

        recyclerView =findViewById(R.id.recyclerView)
        DBHelper = DBHelper(applicationContext)//this)

        //get data customer in database local
        var listCustomerPojo : MutableList<CustomerPojo> = ArrayList()
        listCustomerPojo = SQLCustomer().getCustomerList()

        //show data customer in Recycle Card View
        showListDataToRecycleCardView(listCustomerPojo);
    }

    override fun onCreateOptionsMenu(menu: Menu): Boolean {
         menuInflater.inflate(R.menu.main, menu)
         val searchItem = menu.findItem(R.id.menu_search)
         if(searchItem != null){
              val searchView = searchItem.actionView as SearchView
              val editext = searchView.findViewById<EditText> 
                           (androidx.appcompat.R.id.search_src_text)
              editext.hint = "Search here..."

              searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
                  override fun onQueryTextSubmit(query: String?): Boolean {
                      return true
                  }

              override fun onQueryTextChange(newText: String?): Boolean {
                  var listCustomerPojo: ArrayList<CustomerPojo> = ArrayList()
                  if (newText!!.isNotEmpty()) {
                      val search = newText.toLowerCase()
                      listCustomerPojo = SQLCustomer().getCustomerByName(newText)
                   }
                   showListDataToRecycleCardView(listCustomerPojo);
                   return true
               }
          })
       }
      return super.onCreateOptionsMenu(menu)
   }

   fun showListDataToRecycleCardView(listCustomerPojo: MutableList<CustomerPojo>){
         var customerListDC = arrayListOf<CustomerDataModel>()
         for (i in 0 until listCustomerPojo.size) {
              var custPojo: CustomerPojo = listCustomerPojo[i]
              val  cdm = CustomerDataModel(R.drawable.shop_on_ballon_36,
                                           custPojo.custId.toString(),
                                           custPojo.custCode.toString(),
                                           custPojo.custName.toString(),
                                           custPojo.address.toString())
             customerListDC.add(cdm)
          }

          val recyclerAdapter = CustomerListRecyclerAdapter(customerListDC)
          recyclerView.apply {
                              adapter = recyclerAdapter
                              addItemDecoration(DividerItemDecoration(CommonCtx.getContext(), 
                                                DividerItemDecoration.VERTICAL))
                              setHasFixedSize(true)
           }
     }

     override fun onSupportNavigateUp(): Boolean {
        finish()
        return true
     }

}

file CustomerListRecyclerAdapter.kt

class CustomerListRecyclerAdapter(private var customerListData: ArrayList<CustomerDataModel>) 
                            : RecyclerView.Adapter<CustomerListRecyclerAdapter.ViewHolder>(){

      var appContext: Context? = null
      
      override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
           val layoutInflater = LayoutInflater.from(parent.context)
           val view = layoutInflater.inflate(R.layout.recycler_customer_list, parent, false)
           appContext = parent.context
           return ViewHolder(view)
       }

       override fun getItemCount(): Int = customerListData.size

       override fun onBindViewHolder(holder: ViewHolder, position: Int) {
            val movie = customerListData[position]
             holder.bind(movie)
       }

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
         val intent: Intent? = null
         init {
                itemView.setOnClickListener {
                      var textvCustId   = itemView.findViewById<TextView>(R.id.textvCustId)
                      var textvCustName = itemView.findViewById<TextView>(R.id.textvCustName)
                }

         } 
    
      fun bind(model: CustomerDataModel) {
          var imgViewIcon   = itemView.findViewById<ImageView>(R.id.imgViewIcon)
          var textvCustId   = itemView.findViewById<TextView>(R.id.textvCustId)
          var textvCustCode = itemView.findViewById<TextView>(R.id.textvCustCode)
          var textvCustName = itemView.findViewById<TextView>(R.id.textvCustName)
          var textvAddress  = itemView.findViewById<TextView>(R.id.textvAddress)
          var btnCreateSO   = itemView.findViewById<Button>(R.id.btnCreateSO)
          var btnCreateSR   = itemView.findViewById<Button>(R.id.btnCreateSR)

          imgViewIcon.setImageResource(model.iconPict)
          textvCustId.text   = model.custId
          textvCustCode.text = model.custCode
          textvCustName.text = model.custName
          textvAddress.text  = model.address

          textvCustId.setVisibility(View.INVISIBLE)
          btnCreateSO.setOnClickListener {
                SalesOrderCreateDataFragment().getCustomer(textvCustId.text.toString())
          }
      }
   }//end class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
}

File CommonCtx.kt

   @SuppressLint("StaticFieldLeak")
   abstract class CommonCtx {
        companion object {
        @Volatile
        private lateinit var appContext   : Context
        private lateinit var mainActivity : Activity
        private lateinit var myActivity   : Activity
        private lateinit var myView       : View
        private lateinit var myView2      : View
        private lateinit var myView3      : View
        private lateinit var myContainer: ViewGroup

        fun setContext(context: Context) {
            appContext = context
        }

        fun getFilesDir():String {
           return appContext.filesDir.absolutePath
        }

        fun getOpenFileInput(fileName : String): FileInputStream {
           return appContext.openFileInput(fileName)
        }

        fun getOpenFileOutput(fileName : String): FileOutputStream {
           return appContext.openFileOutput(fileName, MODE_PRIVATE)
        }

        fun getContext():Context{
           return appContext
        }

        fun getContext2():Context{
           return appContext.applicationContext
        }

        fun setMainActivity(activity: Activity) {
           mainActivity = activity
        }
        fun getMainActivity():Activity{
           return mainActivity
        }

        fun setActivity(activity: Activity) {
           myActivity = activity
        }
        fun getActivity():Activity{
           return myActivity
        }

        fun setViewXml(view : View) {
            myView = view
        }
        fun getViewXml():View{
           return myView
        }

        fun setViewXml2(view: View?) {
            if (view != null) {
               myView2 = view
            }
        }
        fun getViewXml2():View{
            return myView2
        }

        fun setViewXml3(view: View?) {
           if (view != null) {
               myView3 = view
           }
        }
        fun getViewXml3():View{
            return myView3
        }

        fun setContainer(container : ViewGroup) {
             myContainer = container
        }
        fun getContainer():ViewGroup{
             return myContainer
        }
    }
  }

File AndroidManifest.xml

     <?xml version="1.0" encoding="utf-8"?>
         <manifest xmlns:android="http://schemas.android.com/apk/res/android"
         package="com.myapp.sfa">
         <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
         <uses-permission android:name="android.permission.ACCESS_MEDIA_LOCATION" />
         <uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
         <uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" /> 
         <uses-permission android:name="android.permission.INTERNET" />
         <application
             android:allowBackup="true"
             android:icon="@mipmap/ic_launcher"
             android:label="@string/app_name"
             android:networkSecurityConfig="@xml/network_security_config"
             android:roundIcon="@mipmap/ic_launcher_round"
             android:supportsRtl="true"
             android:theme="@style/Theme.MyAppSFA">

             <activity android:name=".view.MainActivity">
                 <intent-filter>
                     <action android:name="android.intent.action.MAIN" />
                     <category android:name="android.intent.category.LAUNCHER" />
                  </intent-filter>
             </activity>
             <activity android:name=".ui.view.NetworkSettingActivity"              
                       android:screenOrientation="portrait" />
             <activity android:name=".ui.view.salesman.SalesmanProfileActivity" 
                       android:screenOrientation="portrait" />
             <activity android:name=".ui.view.sales.SalesOrderActivity" 
                       android:launchMode="singleTop"/>
             <activity android:name=".ui.view.sales.SalesReturnActivity"/>
             <activity android:name=".view.ui.customer.CustomerActivity"  />
             <activity android:name=".view.ui.list.CustomerListActivity"  
                       android:noHistory="true"/>
             <activity android:name=".view.ui.list.ProductItemListActivity" />
         </application>
   </manifest>

Problem , while I click button Create SO in page CustomerListActivity Page CustomerListActivity is not Closed and back to Page SalesOrderActivity

Thanks

enter image description here

3 Answers 3

0

in this code, file CustomerListRecyclerAdapter.kt is not working, CustomerListActivity.kt not close

btnCreateSO.setOnClickListener {
   SalesOrderCreateDataFragment().getCustomer(textvCustId.text.toString())
}
1
  • Please stop adding answers as if you are editing your question. Editing the original question by clarifying information is the way to go here.
    – ImAtWar
    Aug 7, 2021 at 6:31
0

this is my file build.gradle(:app)

plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'kotlin-kapt'
}
android {
    compileSdkVersion 30
    buildToolsVersion "30.0.3"
defaultConfig {
    applicationId "com.myapp.sfa"
    minSdkVersion 16
    targetSdkVersion 30
    versionCode 1
    versionName "1.0"
    multiDexEnabled true
    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    }
}
compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
    jvmTarget = '1.8'
}

buildFeatures {
    viewBinding true
}
dataBinding {
    enabled = true
}


sourceSets {
    main {
        res.srcDirs = [
                'src/main/res',
                'src/main/res/layout',
                'src/main/res/layout/fragments',
                'src/main/res/layout/salesaction',
                'src/main/res/layout/salesman',
                'src/main/res/layout/setting',
                'src\\main\\res', 'src\\main\\res\\layout\\itemrecycler', 'src\\main\\res\\layout\\product', 'src\\main\\res\\layout\\customer'
                      ]
    }
    //debug {
    //    res.srcDirs = ['resources/debug']
    //}
}
}

dependencies {
    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation "org.jetbrains.anko:anko:0.10.8"
    implementation 'androidx.core:core-ktx:1.3.2'
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'

    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.9'
    implementation 'com.squareup.retrofit2:retrofit:2.9.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.6.1'
    implementation "com.squareup.retrofit2:converter-moshi:2.4.0"
    implementation 'com.squareup.okhttp3:logging-interceptor:4.4.0'
    implementation 'com.squareup.okhttp3:okhttp:4.9.0'
    implementation 'com.jakewharton.picasso:picasso2-okhttp3-downloader:1.1.0'

    implementation 'com.google.code.gson:gson:2.8.6'
    implementation 'androidx.lifecycle:lifecycle-common:2.2.0'
    implementation 'androidx.lifecycle:lifecycle-runtime:2.2.0'
    implementation 'android.arch.lifecycle:extensions:2.2.0'
    implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.2.0'
    implementation "androidx.viewpager:viewpager:1.0.0"
    implementation 'com.google.android.material:material:1.3.0'
    implementation 'androidx.appcompat:appcompat:1.3.0-rc01'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'androidx.cardview:cardview:1.0.0'
    implementation "androidx.recyclerview:recyclerview:1.1.0"
    implementation 'com.github.bumptech.glide:glide:4.9.0'
    implementation 'androidx.multidex:multidex:2.0.1'
    implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
    androidTestImplementation "org.jetbrains.kotlin:kotlin-test- 
    junit:$kotlin_version"
}

any other ideas, should i change the way to display the Customer list?

Thanks

0

i have add code (itemView.context as Activity).finish() in CustomerListRecycleAdapter.kt

btnCreateSO.setOnClickListener {
      SalesOrderActivity().selectedTabForUpdate(textvCustId.text.toString())
      (itemView.context as Activity).finish()
}

and this work, Customer List close Thanks all

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.